2

I have added json.jar(class file,json.jar,java file are in same folder) and compile my java program using
javac -cp json.jar J.java
It compiles without any error.
This is my program.

import org.json.JSONObject;
import org.json.JSONException;
class J {
   public static void main(String args[]) throws JSONException {
      JSONObject obj = new JSONObject();
      obj.put("name", "foo");
      System.out.print(obj);
   }
}

Then i try to run using
java -cp . J
and i get the following error.

Error: A JNI error has occurred, please check your installation and try again
 Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
            at java.lang.Class.getDeclaredMethods0(Native Method)
            at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
            at java.lang.Class.privateGetMethodRecursive(Unknown Source)
            at java.lang.Class.getMethod0(Unknown Source)
            at java.lang.Class.getMethod(Unknown Source)
            at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
            at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: org.json.JSONException
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
paulcab
  • 1,102
  • 1
  • 9
  • 16
  • may be you need to define the exact path : read here http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath – lesnar Nov 10 '16 at 14:54
  • Thanks. it's not about defining path. I have added the solution as an answer. – paulcab Nov 10 '16 at 14:57

2 Answers2

1

Use java -cp "json.jar;" J instead of java -cp . J
Add the jar file in order to run too.

paulcab
  • 1,102
  • 1
  • 9
  • 16
0

try java -cp json.jar J.class. and also see the java documents.

Rakesh Varma
  • 151
  • 1
  • 2
  • 14