This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Jar - Manifest.mf - Class-Path Attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Jar - Manifest.mf - Class-Path Attribute" Watch "Jar - Manifest.mf - Class-Path Attribute" New topic
Author

Jar - Manifest.mf - Class-Path Attribute

amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 513
Hello,
I trying to build an executable jar that can be run using "java -jar myapp.jar" command.
I have a dependency on lot of JARs like log4j.jar , jdbc driver jar file and others. As per my knowledge I created the Manifest.mf file that contains "Main-Class" and "Class-Path" attributes. Class-Path attribute points to the log4j.jar in the lib folder.

Here is how my JAR file looks like
<pre>
myapp.jar
|---MyMainClass.java
|---meta-inf
|---lib
|----log4j.jar
|----ojdbc.jar
|----ibatis.jar
|----json.jar
|----Other JAR Files
[/pre>

My Manifest.mf has the Class-Path attribute as below
Class-Path: lib/log4j.jar lib/ojdbc.jar lib/ibatis.jar lib/json.jar

I thought after doing this my application will work file. But its throwing the NoClassDefFoundError when runs for Log4J classes.

Thanks in Advance,
amit
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Amit:

It's not general practice to put .jar files in other .jar files. The standard Java class loader can't find .class files stored in .jar files that are stored in .jar files. It is possible to write a custom class loader to do that, but I haven't done that myself, so I can't give you any details.

John.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 513
Hi,
thanks for the reply.
Yeah I read that note in Java tutorial.
But then not sure of how this Class-Path attribute would work if it can not be used to refer the dependent JARs which are packed in the application.

Regards,
Amit
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Amit:

The .jar files it refers to are external. For example, if your .jar file is called App.jar and the classpath is set to something like "Dep1.jar Dep2.jar", you put the Dep1.jar and Dep2.jar in the same directory as App.jar.

John.
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 513
Hello.
Thank you very much John about the explanation.
I got the missing link in my understanding of the note.
I was packaging the dependencies in my Jar file itself and thats where the "Note" in tutorial comes into the picture. I was thinking about "literary" external URL.

Anyways thanks once again,
Amit
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Amit:

No problem. Glad I could help .

John.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Amit,

I recently tried to do the same and had to learn that the classpath in the manifest is not really what you would intentionally think when you package JARs into another JAR file

I had used the Maven assembly plugin to create the "executable" together with all dependencies and after some experimenting I found out how you get it to work without the need to deliver additional JAR files. The easiest way is to unpack all dependency JARs and put the raw class file with the usual package/directory structure into the JAR. Then

Of course it's another story if that is a good or bad practice to deploy your application this way together with external dependencies. But that's your personal decision.

Marco
 
GeeCON Prague 2014
 
subject: Jar - Manifest.mf - Class-Path Attribute