Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between sourcepath and just specifying the source file?

Tags:

java

What is the point of the javac -sourcepath option?

Say you have a file Ball.java, and file Eight.java, there seems to be no difference between running:

javac Eight.java lib/pool/Ball.java

and

javac Eight.java -sourcepath lib

The classpath makes sense to me as it means you just need to distribute class files to developers who won't need to mess with the source, but I would think you're only likely to have/need the source if it's your own/an internal project so can't understand why it's needed.

Do people have large repositories of source code instead of classes?

From further testing I've confirmed that -sourcepath doesn't even take the last modified date of the source into account like the classpath, so couldn't even conceivably save time during building. As when using -classpath, this will build the .class file if it doesn't exist and the source does, or if the .java file is newer than the .class file, whereas any source files on the sourcepath will be built again irregardless of how new they are. And when -sourcepath and -classpath are both specified then it takes the action of the classpath to only rebuild if the source files are newer, so specifying the sourcepath and classpath appears to be completely pointless. I've also tested to make sure that -sourcepath and -classpath both only build the necessary source files needed for resolution. The only upside from -sourcepath over specifying the specific .java files I can find is that sourcepath just has to have a directory specified, and any jar or zip files are automatically extracted.

So is the point of -sourcepath due to laziness (not wanting to extract & specify all the source files)? And is there any upside to using -sourcepath over -classpath? Since they appear to do the same thing but classpath performs it better by saving time when the source doesn't need rebuilding.

like image 935
lavamunky Avatar asked Mar 22 '26 13:03

lavamunky


1 Answers

The sourcepath is used to locate .java files for classes referenced in the files you asked to compile (Eight.java in your example) for which no .class file exists. The compiler will automatically compile those too. I'm not sure if it compares the modified-time of the .java and corresponding .class file and recompiles the .java if it is newer, but I think it does.

like image 64
Adrian Pronk Avatar answered Mar 24 '26 02:03

Adrian Pronk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!