This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Package several jars lib in one single jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package several jars lib in one single jar file" Watch "Package several jars lib in one single jar file" New topic
Author

Package several jars lib in one single jar file

Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
Hello everybody !

I have not write a simple (Swing) java application since a long time, and I have a doubt !
The application use three external jar files.
Can I package the application so it is only include in ONE jar file (easier to deploy)?
Thanks,

Olivier
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Sure. Adding jars to a single jar is done using the jar command. Suppose you have jarA, jarB and jarC. For your deployment you would need a manifest file too.


The manifest would specify the external jars' full path.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
Thanks very much K!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
I disagree. Yes, you can package jar files inside another jar file, but the standard class loader will not look into any of those jar files when it is trying to find a class to load.
You could write your own classloader to load classes from those jars but it is probably more effort than just redistributing all the jar files separately.

I have a vague recollection that there may be a 3rd party classloader available that will allow you to do this. Try searching these forums for the many times this question has been asked before.

Joanne
Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
Yes you are right Joanne, the class loader does not find the classes inside the included jar files.

So, if I understand correctly, without playing with class loaders (ie modifying the application), it is NOT possible to package all the jar files into a unique jar file. It is true ?

Thanks,

Olivier
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Correct. However, you can package them into one sub folder, then include that sub folder in the class path:
Structure:
You can of course also skip the sub folder; your class path will then be as K. Tsang mentioned.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Olivier Scalbert
Greenhorn

Joined: May 07, 2004
Posts: 25
Thanks,

As I will not be able to start the application like this:
java -jar application.jar
without playing with class loader, I will forget this approach.

By the way, why the standard class loader does not look into the included jar files ?
Is there a good reason ?
Finally what is the purpose of the "Class_path" entry ?

Thanks,

Olivier
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Olivier Scalbert wrote:As I will not be able to start the application like this:
java -jar application.jar
without playing with class loader, I will forget this approach.


You will need to distribute the jar files separately whichever way you choose to launch your program. Using the -jar option means you can specify the classpath in the manifest file of your application jar and don't need to specify it on the command line.

Olivier Scalbert wrote:By the way, why the standard class loader does not look into the included jar files ?
Is there a good reason ?


Don't know I'm afraid. Probably just to avoid making the class loading process too complicated.

Olivier Scalbert wrote:Finally what is the purpose of the "Class_path" entry ?


When you use the -jar option, the JVM uses the Class_path" entry to find any classes it needs to load.
IF you don't use the -jar option then you need to specify your classpath on the command line with the -cp option.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

This might be a useful tool: One-JAR.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Jesper Young wrote:This might be a useful tool: One-JAR.


Thanks Jesper. That was the 3rd party tool I was thinking of. Just couldn't remember the name.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Joanne Neal wrote:IF you don't use the -jar option then you need to specify your classpath on the command line with the -cp option.

Actually, strangely enough (?), if you use -cp, it will still use the Class-Path inside the JAR files.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package several jars lib in one single jar file
 
Similar Threads
packing an acces file with my jar
RMI UnMarshal Exception.
Packaging 3rd party .jars
Help with classpath inside JAR
Jar files & JarOutputStream