0

I have written a peiece of code that references a JSON library in my Eclipse.

When trying to Export to Jar file, I chose the option Export Java source files and resources.

My code is dependent on JSONObject library.

Now I have packaged all of my files and when I run the jar file on a linux machine, I get the ERROR::

# java -cp SendCommand.jar ExecuteShellComand Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject at ExecuteShellComand.main(ExecuteShellComand.java:19) Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more 

Unzipping my jar file yields.

# tree . ├── commons-io-2.5.jar ├── ExecuteShellComand.class ├── ExecuteShellComand.java ├── json-simple-1.1.jar ├── SendCommand.jar └── META-INF └── MANIFEST.MF 1 directory, 6 files 

Although all the required files are present, I still get the ERROR. What am I missing here ?

2 Answers 2

1

You need to separate out the json-simple-1.1.jar from SendCommand.jar.

Instead copy json-simple-1.1.jar and other dependent JARs into your current directory so that you can confirm that executing the following modified command works:

# java -cp SendCommand.jar:commons-io-2.5.jar:json-simple-1.1.jar ExecuteShellComand 
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately this did not work. Same error. My .classpath states <classpathentry kind="lib" path="json-simple-1.1.jar"/>
Keep in mind that you could choose to run ExecuteShellComand app in two ways: (1.) using the Eclipse IDE or (2.) using the command line. The .classpath file entries which you listed in your comment are intended for the Eclipse IDE; they will not be read when executing from the command line which is what my answer was focused on.
0

Classpath reference: Right click on project in Eclipse -> Buildpath -> Configure Build path -> Java Build Path (left Pane) -> Libraries(Tab) -> Add External Jars -> Select your jar and select OK.

Deployment Assembly: Right click on WAR in eclipse-> Buildpath -> Configure Build path -> Deployment Assembly (left Pane) -> Add -> External file system -> Add -> Select your jar -> Add -> Finish.

Comments