user2911290 user2911290 - 1 year ago 133
Java Question

Regarding automatic recompilation, I can't spot the difference between javac's -classpath and -sourcepath options

I'm trying to understand the difference between javac's

options, regarding automatic recompilation of source code files. I read the Java documentation on the subject of javac, and for its
option it stated -

Note: Classes found through the class path may be subject to automatic recompilation if their sources are also found

I also looked at this webpage (, to learn more about javac's
options. In their example, it describes a test project involving two java files called (a superclass), and (a dependant subclass). It gave three options on how to compile this test project, two of which are summarized below.

Option #2

  • Add TestFoo to the sourcepath while compiling TestBaz:

    javac -d baz/classes -sourcepath foo/src baz/src/

Option #3

  • Put dependencies in both sourcepath and classpath. Then, if the .class file in the classpath goes out of date, the .java file in sourcepath will be recompiled.

Unfortunately, I am unable to spot the difference between these two options. For instance, in option #2 (which doesn't use -classpath), if I edit either or, then I will get an updated TestFoo.class or TestBaz.class file. In other words, if either .class file goes out of date, then the source code will be recompiled. In option #3 above (which does use -classpath), the same thing will occur. Consequently, I can't see a difference between options #2 and #3.

Can someone please outline the structure of one simple example test project where I can see two slightly different javac commands being used with it? One javac example command should use
, while the other should not. When the javac example that includes
is used, automatic recompilation occurs if the edited superclass source file is found. When the javac example that excludes
is used, automatic recompilation does not occur if the superclass source file has been edited. Thanks a lot.

Answer Source

I've experimented with various javac command line examples, and I can now answer my own question:

If any source code dependency files are edited, then either of these two javac variations will recompile them:

javac -sourcepath src -cp bin -d bin src\pkgs\main\
javac -sourcepath src -d bin src\pkgs\main\

However, the following javac variation will not recompile edited source code dependency files:

javac -cp bin -d bin src\pkgs\main\

Please note that in all three of the javac variations above, the Main.class file is always regenerated. In my original question, I wondered if it was possible for automatic recompilation to not occur, if javac excluded the use of -cp (-classpath). I don't think that is possible, because if javac was not told about the location of .class files, it would have be told about the location of source files, and once found, it would then have to recompile them, in order to create the required .class files for the project!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download