• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Package several jars lib in one single jar file

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much K!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's a tiny ad. At least, that's what she said.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic