0

I'm trying to run java and javac from the command line, but I'm having trouble setting the CLASSPATH for the javac sdk tool.

I've successfully added the CLASSPATH variable through the windows environmental variable settings, and this works for the java command. I am able to execute class files from any directory in the command line. However, when trying to use the javac command (where the .java files are in the same CLASSPATH directory), i receive the error message that the file is not found.

My CLASSPATH variable is set to:

C:\Users\ejovo\OneDrive\Documents\Coding\Java>

Here are 3 examples of what I mean.

C:\>javac MyFirstApp.java
javac: file not found: MyFirstApp.java
Usage: javac <options> <source files>
use -help for a list of possible options

C:\Users\ejovo\OneDrive\Documents\Coding\Java>javac MyFirstApp.java

C:\>java MyFirstApp
Hello World

We see that the java command can be run from anywhere while the javac still has to be run from the directory in which the .java files are.

I've tried setting the javac CLASSPATH with the -cp and -classpath options without any luck:

C:\>javac -cp C:\Users\ejovo\OneDrive\Documents\Coding\Java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options

C:\>javac -classpath C:\Users\ejovo\OneDrive\Documents\Coding\Java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options

And I've already set a PATH variable that makes the java and javac commands run correctly

I've also tried changing the sourcepath with the -sourcepath argument:

C:\>javac -sourcepath C:\Users\ejovo\OneDrive\Documents\Coding\Java
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options

Despite that, I clearly do have a .java source file present:

 Directory of C:\Users\ejovo\OneDrive\Documents\Coding\Java

07/25/2019  03:08 PM    <DIR>          .
07/25/2019  03:08 PM    <DIR>          ..
07/25/2019  03:29 PM               425 MyFirstApp.class
07/24/2019  06:40 PM               127 MyFirstApp.java
               2 File(s)            552 bytes
               2 Dir(s)  57,735,630,848 bytes free

Let me know if anyone has any other ideas!

ejovo13
  • 3
  • 2
  • I believe this question is answered here, https://stackoverflow.com/questions/6623161/javac-option-to-compile-all-java-files-under-a-given-directory-recursively – riccio Jul 25 '19 at 19:42
  • 2
    The CLASSPATH is only used for finding class files, not Java source files. – David Conrad Jul 25 '19 at 19:51

1 Answers1

1

It appears that you are getting a bit confused here. The CLASSPATH is used to tell the java and javac programs where to find compiled .class and .jar files.

You are attempting to use the CLASSPATH to have javac locate SOURCE files, which does not work.

See here: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html

You might wish to examine the -sourcepath argument.

Jamie
  • 1,786
  • 1
  • 16
  • 20
  • Which totally makes sense, however I had read that the java compiler searches through the classpath for src files AND class files. Nevertheless, I had already tried setting the sourcepath with -sourcepath to no luck. Ill edit the original post to show you what I mean – ejovo13 Jul 26 '19 at 20:09
  • In your final example, you set the source path, but then you did not specify any files to compile! – Jamie Jul 28 '19 at 13:04