2

I create a new jar file. when i run the jar using

java -jar UpdateAvailable.jar com.dao.mysql.UpdateAvailable

I got the following exception.

Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jettison/json/JSONException
Caused by: java.lang.ClassNotFoundException: org.codehaus.jettison.json.JSONException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

But I copied all the supporting jars inside the UpdateAvailable.jar. Now what my requirement is run the jar file with out specifying supporting jars, like

java -jar UpdateAvailable.jar com.dao.mysql.UpdateAvailable
Bathakarai
  • 1,517
  • 6
  • 23
  • 39
  • Are you saying that you have stuffed all the dependent jars inside the `UpdateAvailable.jar`? – maba Sep 25 '12 at 12:33
  • Look at [this](http://stackoverflow.com/questions/12229300/creating-an-independent-jar-file/12230160#12230160) – Roman C Sep 25 '12 at 12:34

3 Answers3

0

Here are some points to remember while you are working with JAR files and ClassPath: -

  • For making your JVM find class, your classpath should be set till the directory containing the class. In this case you have JAR.
  • So, you need to give the path till your Jar File Name
  • Now, since you have your Jar files inside your currently running Jar file, you can't set your classpath till the folder inside a Jar, so basically you won't be able to set classpath for inner Jar Files..
  • So, clearly JVM won't be able to see classes inside your inner Jar..
  • So, make sure that your required JAR files are outside of all the JAR files (I mean to say - avoid Nested JAR Files)..
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

I'm not sure how you are packaging your jar file, but it would sound like you need something like One-Jar to help you package your file and all your supporting libraries into a single jar. One-Jar comes with it's own classloader that allows it to load all the included jars in your executable jar file.

Configuration is fairly straight forward and easy to use. If you are using Maven, you can use the one-jar maven plugin to help you configure which libs you want included.

There are also other similar packages you can use instead of One-Jar, such as JarJar and ProGuard. Also take a quick look at this SO question.

Community
  • 1
  • 1
Eric B.
  • 23,425
  • 50
  • 169
  • 316
0

In executable jar couldn't reads any supporting jar files inside a jar. So keep all supporting jars in a folder and place the executable jar in the same directory. And add "Class-Path" as "Supporting jar files separate with space" in "MANIFEST.MF" File. Now run the jar as

java -jar UpdateAvailable.jar com.dao.mysql.UpdateAvailable
Bathakarai
  • 1,517
  • 6
  • 23
  • 39