这是我的java代码
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PlutoMake {
public static void main(String[] args) throws JSONException {
}
}
然后我用这个编译它并且编译没有错误
javac PlutoMake.java -cp java-json.jar
现在我从 Windows 的 eclipse 复制了这个项目,并使用那个 jar 它工作正常。我可以访问所有 json 函数。但是在 linux 上,当我运行它时,我得到了
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)
如果我删除 throws JSONException
然后我可以运行它。所以出于某种原因我可以导入 jar 但我无法访问它的任何功能/异常而不会使我的程序崩溃......
Jar 文件来自此链接的答案 Importing JSON into an Eclipse project
有人知道如何解决这个问题吗?
谢谢
最佳答案
将jar文件添加到运行时类路径
java -cp java-json.jar:. PlutoMake
关于java - 在 linux 的 java 文件中导入 json jar 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32038532/