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
|----Other JAR Files
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.
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.
Joined: May 14, 2004
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.
John de Michele
Joined: Mar 09, 2009
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.
Joined: May 14, 2004
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.
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.