开发者

NoClassDefFoundError with Executable Jar

Probably a very usual problem with exec jar. I am trying to create and run an executable jar and its driving me crazy.

I have couple of classes(with package stmts), one of which has a main method, a simple empty constructor and ofcourse few biz methods, these form part of a small eclipse project.

I am bundling both these and a manifest file into a jar(arc.jar) using a build.xml. My program uses jdom library and also references from one of my other eclipse projects, so I am including both the jdom libray and other biz library while building my arc.jar.

This is the Manifest.MF I wrote, there is a new lineafter Main-Class

Manifest-Version: 1.2  
Class-Path: jdom.jar other.jar
Main-class: uk.co.Art 

When I unzip this arc.jar, it contains jdom.jar, other.jar, META-INF/Manifest.mf and the package with my classes. Funnily, the Manifest file in jar looks like this -

Manifest-Version: 1.2 
Created-By: 14.2-b01 (Sun Microsystems Inc.) 
Main-class: uk.co.Art
Class-Path: jdom.jar other.jar

I got the ClassNotFoundException ClassNotFoundException: uk.co.Art when the value of Main class was without the package. With package the error changed to ClassNotFoundException: org.jdom.JDOMException.

So just for testing purpose, I tried giving the fullpaths with c:\ for both my executing class and the jars but still it is not able to locate the main class, errors out ClassNotFoundException: uk.co.Art.

I cant seem to understand why.

The command line I am using is - java -jar Arc.jar

The version on cmd prompt is -

java version "1.6.0_16"

Java(TM) SE Runtime Environment (build 1.6.0_16-b01)

Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode)

while the eclipse installed jre is jre6

What can you folks suggest ?

Alex' solution did solve the problem for local execution.

I am also thinking of another way(as an extra option) of execution, to deploy the Arc.jar on server and run a batch on that machine which basically sets the java classpath and then calls either the jar or directly the class whichever way it works. But its only an option, not a preferre开发者_高级运维d one though.

Or, also, set that classpath in my manifest and build jar locally and deploy on the server.

Would like to try out a few things though. But now I understand that the basic problem was jars into jar and hence classpath issue.

Thanks a lot folks.


You cannot bundle other JAR's in a JAR. They have to go in the same folder or at least in a folder relative to the Arc.jar. You specify then its relative path in the Class-Path entry.

Eclipse however has an useful export tool which allows you to embed the contents of other JAR's in your JAR so that you don't end up with multiple loose JAR's which are required to be placed in a specific location. Check the 2nd Library Handling option when you choose Export > Runnable JAR file.

NoClassDefFoundError with Executable Jar

When choosing Package required libraries into generated JAR, then Eclipse will add a special classloader which will load those JAR's transparently for you before executing the main class. It's doing that with help of JarRsrcLoader.


You do not have to pack your additional jars into your jar. These jars should be in the same directory as your application jar. By other words: put all jars into one directory and run. Everything will work.

Reasons: think what does executable jar mean. If you jar is not "executable" you run it as following:

java -cp art.jar;ldom.jar;other.jar uk.co.Art

when your jar is executable you use the following command line:

java -jar art.jar

No magic: jvm opens manifest and takes name of main class and other jars from there and virtually creates command line like first one. Now you understand that all jars in your case must be in the same directory.


Maybe the .jar is corrupt, like sometimes happened to me. Take a look at this.


Just couple of things to be noted when exporting.

  1. JRE associated to the project is consistent with the JRE that would be used to run it.

  2. Please export your project into a local drive, then move it into any shared drive (this fixed my issue).

Thank you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜