Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package several jars lib in one single jar file

 
Olivier Scalbert
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3502
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Olivier Scalbert
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much K!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Olivier Scalbert
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Olivier Scalbert
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be a useful tool: One-JAR.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic