Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jar - Manifest.mf - Class-Path Attribute

 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit:

No problem. Glad I could help .

John.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic