Suppose I have a Felix class & a Rover class.
Each class is very simple
I now put those 2 animal source files into a jar
and delete the animals directory.
I also have a GreetAnimals class that uses those two animals.
I can compile the GreetAnimals source, giving the jar file as the -sourcepath
which compiles without error, generating three .class files.
Now I have problems:
1) As my -sourcepath didn't include the current directory, I wasn't expecting javac to even find GreetAnimals.java. I think that's how -classpath behaves when invoking programs with java, where .class files are in jar.
2) The generated Felix.class & Rover.class are both put in the current directory. I was expecting directories to get created matching the package structure.
3) Attempting to run GreetAnimals results in a NoClassDefFoundError for Felix with ... 1 more, presumably for Rover.
So, is it not allowed to put source in jars & expect javac to use -sourcepath to find it?
Paul Anilprem wrote:If you don't use the -d option while compiling source files, the resulting class files will not be put in appropriate package driven directory structure. This has nothing to do with sourcepath option.
Try adding -d . (that is a dot), to your javac command and see what happens.
Thank you Paul. Yes, that works as expected now.
I'd assumed that without any '-d .' javac would default to a package appropriate structure in the current directory.
The reason I'd thought that, is that if my files are
then compiling with no -d option results in all the class files being put in the package appropriate directory. I guess that what it's actually doing, in the absence of any -d, is putting each .class file in the same place as the corresponding .java source file. In this case, I have my source in a package appropriate directory structure to start with.