This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

can source files be stored in a jar?  RSS feed

 
Ranch Hand
Posts: 205
13
Notepad
  • 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?
 
Enthuware Software Support
Rancher
Posts: 4202
35
  • 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: 205
13
Notepad
  • 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!