You are not logged in.

#1 2005-06-24 17:39:01

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Deploying Java apps: Jars within a jar

Dear Java gurus,

Imagine I have a Java application. I wish to bundle it all within a jar file. The problem is, I also wish to add some dependencies, in the shape of a couple of other jars. This means that the user could run the app using the straight-forward "java -jar myapp.jar" command.

So, I jar it all up. This Jar contains a MANIFEST.MF that specifies the main class, and also the classpath. ("Class-Path: lib/other.jar") as the extra jars were in a dir called lib within the jar.

But, this doesn't work run as java complains about not being able to resolve classes that exist within other.jar.

I have yet to find anything on the net beyond really basic examples of using jar. Anyone had more experience with it and can share some tips?

Offline

#2 2005-06-24 17:48:23

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Deploying Java apps: Jars within a jar

Its a horrible thing. Basically, you can't include jars in jars.  I asked this question on the Java.sun.com forums a while ago if you want to search there. Its a common question and pisses people off.

You have some other options:

1: extract all dependency jars and include the classes in a single jar file.

2: distribute multiple jars and set the Class-Path in the Manifest to point to them. Since you're pointing at lib/other.jar, you gotta use:

myjar.jar
lib/other.jar

You won't find another way around this. I recall somebody suggesting that packaging jars with jars should be available in mustang, but as of yet, it is not so.

Also, I would strongly recommend starting to use Java Webstart /JNLP for distributing java apps. I've never done it, but it takes all this hassle out for you by automatically maintaining dependencies. I'm intending to try to get Ensmer going using JNLP sometime.

Dusty

Offline

#3 2005-06-24 17:51:01

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Deploying Java apps: Jars within a jar

Doesn't matter. I've finally found from Sun's website a nugget which says that it can't be done from the Manifest. You need to bootstrap with a start-up class which uses ClassLoaders to loadup all the necessary classes within the jar.

Found One-Jar which looks like it will do the job.

Offline

#4 2005-06-24 17:56:31

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Deploying Java apps: Jars within a jar

Dusty wrote:

Also, I would strongly recommend starting to use Java Webstart /JNLP for distributing java apps. I've never done it, but it takes all this hassle out for you by automatically maintaining dependencies. I'm intending to try to get Ensmer going using JNLP sometime.
Dusty

I've had a little experience with WebStart. It's quite nice. Maybe I'll tinker with that then.

Offline

#5 2005-06-24 17:58:22

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Deploying Java apps: Jars within a jar

Hmm... never saw that option, looks kind of inelegant to me, but I guess it would work. I still suggest JNLP. ;-)

Dusty

Offline

#6 2005-06-25 01:33:15

kleptophobiac
Member
From: Sunnyvale, CA
Registered: 2004-04-25
Posts: 481

Re: Deploying Java apps: Jars within a jar

I use fatjar to do the same thing - it's an eclipse plugin that can bundle jars together.

Offline

Board footer

Powered by FluxBB