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

StackOverflow https://stackoverflow.com/questions/23604744

  •  20-07-2023
  •  | 
  •  

Frage

I'm trying to understand the difference between javac's -classpath and -sourcepath options, regarding automatic recompilation of source code files. I read the Java documentation on the subject of javac, and for its -sourcepath 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 (idevelopment.info), to learn more about javac's -classpath and -sourcepath options. In their example, it describes a test project involving two java files called TestFoo.java (a superclass), and TestBaz.java (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/TestBaz.java
    

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 TestFoo.java or TestBaz.java, 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 -classpath, while the other should not. When the javac example that includes -classpath is used, automatic recompilation occurs if the edited superclass source file is found. When the javac example that excludes -classpath is used, automatic recompilation does not occur if the superclass source file has been edited. Thanks a lot.

War es hilfreich?

Lösung

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\Main.java
javac -sourcepath src -d bin src\pkgs\main\Main.java

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

javac -cp bin -d bin src\pkgs\main\Main.java

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!

Andere Tipps

The difference is that -classpath tells the compiler where to find already-compiled classes, and -sourcepath tells the compiler where to find the source for those classes. So you should use the latter for classes you have source code for, and the former for classes you only have the object code (.class file) for.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top