简体   繁体   中英

Getting error when importing json jar in my java file in linux

This is my java code

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PlutoMake {

    public static void main(String[] args) throws JSONException {

    }
}

and I compile it with this and it compiles without error

javac PlutoMake.java -cp java-json.jar

Now I copied this project from eclipse from windows, and using that jar it worked fine. I could access all the json functions. But here on linux, when I run it, I get

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:268)

If I remove the throws JSONException then I can run it. So for some reason I can import the jar but I can't access any of its functions/exceptions without crashing my program...

Jar file is from the answer from this link Importing JSON into an Eclipse project

Does anyone know how to fix this?

Thanks

将jar文件添加到运行时类路径

java -cp java-json.jar:. PlutoMake

在Linux上的eclipse中创建项目后,需要配置buildpath插入jar

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