1.代码如下:
import org.json.JSONObject; import org.json.JSONException; public class testJson{ public static void jsonObjectTest() { JSONObject studentJSONObject = new JSONObject(); try { studentJSONObject.put("name", "Jason"); studentJSONObject.put("id", 20130001); studentJSONObject.put("phone", "13579246810"); } catch (JSONException e) { e.printStackTrace(); } }; public static void main (String[] args){ System.out.println("test json:"); } }
截图:
2.执行方式:
已下载java-json.jar 包,编译通过,但执行报错
在 cmd 内执行
3.错误文本:
D:\javaCode\testJSON>javac -cp java-json.jar testJson.java
D:\javaCode\testJSON>java testJson 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) ... 7 more
4.请问:
(1)不加try catch 报错,网上其他人的怎么不用?
(2)加上了try catch 后程序怎么调通?
(3)缺少包?配置不对?搞不定了,请指教,谢谢
将 java-json.jar 加到环境变量内。
运行OK