0

I downloaded the following link:

http://www.java2s.com/Code/JarDownload/java/java-json.jar.zip

I then added the jar to the build path: Right click the Project > Build Path > Configure build path > Select Libraries tab > Click Add External Libraries > Select the Jar file Download.

I was able to use the java-json library without eclipse giving any errors. However, when I went to export the project to a jar, there was no option to include the java-json Referenced Libraries as part of the jar. So I assumed that by adding the libraries to the Build Path, I did not have to export the external jars (there was no option to do that anyway):

enter image description here

After I created the jar, I ran the program:

java -jar gateway.jar -p 1732

And I got the following error:

Exception in thread "Thread-0" java.lang.NoClassDefFoundError: org/json/JSONException
    at com.guarddoggps.main.UdpServer.run(UdpServer.java:28)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

It seems that the runtime cannot find the class JSONException even though it is clearly in the java-json.jar file I included in the build path and is present in "Referenced Libraries" in Eclipse.

What might I be missing here?

JohnMerlino
  • 3,900
  • 4
  • 57
  • 89

2 Answers2

0

To solve the issue you can use the following command to start you application:

java -cp java-json.jar -jar gateway.jar -p 1732

For this to properly work you will have to the java-json.jar in the same dir as your exported jar.

Viorel Florian
  • 594
  • 9
  • 29
  • Im looking to do this within Eclipse – JohnMerlino Jun 04 '14 at 19:21
  • This is what worked for me: http://stackoverflow.com/questions/502960/eclipse-how-to-build-an-executable-jar-with-external-jar. Plus I had to do this too: http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-java-local-configuration.htm – JohnMerlino Jun 04 '14 at 19:35
0

You might also consider creating a so called fat jar which will contain all the needed classes. For example: http://fjep.sourceforge.net/

If you do not want to go through the hassle of managing all the depencencies by yourself, consider using a build tool like Maven https://maven.apache.org/ or Gradle https://gradle.org/. They will help you manage all the dependency work and even download the required jars from the internet.

Marc Giombetti
  • 819
  • 1
  • 8
  • 20