This week's giveaway is in the Spring forum.
We're giving away four 3 month subscriptions to Marco Behler Videos and have Marco Behler on-line!
See this thread for details.
Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

can source files be stored in a jar?  RSS feed

 
Richard Hayward
Ranch Hand
Posts: 170
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been experimenting with the javac -sourcepath option.

Suppose I have a Felix class & a Rover class.

animals/cats/Felix.java
animals/dogs/Rover.java

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
Enthuware Software Support
Ranch Hand
Posts: 4060
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Hayward
Ranch Hand
Posts: 170
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

GreetAnimals.java
animals/cats/Felix.java
animals/dogs/Rover.java

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!