lavamunky
lavamunky

Reputation: 556

Difference between sourcepath and just specifying the source file?

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.

Upvotes: 3

Views: 570

Answers (2)

lavamunky
lavamunky

Reputation: 556

I've found from further colleagues in development that they would generally use this during development if they want to build part of a project or subproject. Instead of building everything from scratch (which could take hours), they instead simply want to build a few projects, then they can specify other directories of the whole project in -sourcepath so that this can resolve classes. This means it can automatically find what dependencies it needs and build these as and when they are needed.

I've also been informed that this can be helpful during maven builds since the order of dependency gathering can be very unorganised. Instead of having missing dependencies because the jars aren't there at time of building, you can specify the entire code tree as the sourcepath. Although I imagine this isn't the best advice, it's helpful when somebody is having problems building.

Upvotes: 0

Adrian Pronk
Adrian Pronk

Reputation: 13926

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.

Upvotes: 1

Related Questions