Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading JSON file error

Tags:

java

json

I try to read JSON file with Java. The code is:

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("j.txt");
    String jsonTxt = IOUtils.toString( is );

    JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );

    JSONObject routes = json.getJSONObject("routes");
    JSONObject legs = routes.getJSONObject("legs");
    JSONObject distance = legs.getJSONObject("distance");
    String dist = distance.getString("text");
}

And the line JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt ); makes an error:

 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at antmedic.algorithm.SimpleParser.main(SimpleParser.java:24)
 Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
    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:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    ... 13 more
Java Result: 1

I would be grateful for solving this problem.

like image 286
Devel Avatar asked Jan 16 '11 16:01

Devel


2 Answers

Latest version of Commons Lang (currently 3.1) has package name of org.apache.commons.lang3 instead of org.apache.commons.lang. For the later package name (as in your case), you should download 2.6 version of Commons Lang.

like image 99
Xiao Jia Avatar answered Oct 06 '22 14:10

Xiao Jia


Sounds like you don't have apache commons jar file in you class path

like image 35
Amir Raminfar Avatar answered Oct 06 '22 14:10

Amir Raminfar