7

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.

0

4 Answers 4

12

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I was using latest version i.e. 3.1 and was getting this error
+1 Great, I was trying with 3.1 and getting same error. Thanks for this great answer.
5

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

5 Comments

That's a vague question because I am not sure how you have set up your project. But assuming its just a simple java project, look for the missing jar file online and then add it to your class path in netbeans. I don't use netbeans so maybe someone else can help you.
I think I have already did it - I have added the file commons-io-2.0.1.jar as a library to my project. Do I have to add something more?
You need commons lang. Guessing here commons.apache.org/lang/download_lang.cgi
@Amir: can you please tell as why one need to include these JARs for project. whats the logic behind that? your reply will help solicited.
@Sam, if you are using a lib that doesn't depend on anything then you would never need to depend on any other jar files. In this case, you are using a JSON lib that depends on apache common's lib. When packaging these libs, the apache common lib jar files need to be present. But they are not required again until runtime. So when you go to use your JSON lib, it throws these errors because it can't find them. Adding the required jar tells JAVA where these jar files are. Some times it takes hundreds of files to get everything working. I recommend using a dependency manager like maven.
1

You are missing some jar dependency which contains org.apache.commons.lang.exception.NestableRuntimeExceptio class. Look on internet where to find this class and add this jar in your classpath (in this case Apache Common Lang)

Comments

0

You can update your maven pom.xml file with this. I had to specify 2.3 for the version to get it to download.

    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.3</version>
    </dependency>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.