简体   繁体   中英

Java NoClassDefFoundError Exception for JSONObject

I never used JSON so this is the first time. But I cannot even get it to work correctly.

import org.json.*; // get everything !

//method not even done yet... 
public static String getJSON( String jsonSource, String key ) {

    JSONObject obj = new JSONObject( jsonSource );
    return null;
}

This small piece of code will throw an Exception. Compiling it also gave me warnings.

warning: json-20141113.jar(org/json/JSONObject.class): major version 52 is newer than 51, the highest major version supported by this compiler. It is recommended that the compiler be upgraded.

I'm running linux mint 17 with this version of java :

java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.5) (7u79-2.5.5-0ubuntu0.14.04.2)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

Is that the reason why it doesn't work?

JDK 8 = 52 and JDK 7 = 51, the json-*.jar you use has been compiled with JDK 8 and you're compiling your code with JDK 7.

Find another version of json for JDK 7 or compile your code with JDK 8.

By the way, for JSON you should have a look at Jackson

You have to update your JDK. Your dependency uses Java 8, and you try to compile your code with JDK7.

The package you are using has been compiled for JDK 8. See https://github.com/douglascrockford/JSON-java

Fortunately no JDK 8 feature has been used in code. In fact, except for a couple of multi catch statements (JDK 7), code is JDK 5 compatible.

I forked that repository https://github.com/makeroo/JSON-java and recompiled code for JDK 5 (my requirement was JDK 6 actually but I choose minimum code requirement).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM