Reputation:
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 ?
Upvotes: 0
Views: 2468
Reputation: 9418
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
Upvotes: 1
Reputation: 3512
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.
Upvotes: 0