我有一个名为 GameClient.jar 的 jar,它有一个 main 方法。我有一个 json.jar,其中包含 org/json/JSONException.class
这两个都在 dist 文件夹中。
我运行以下命令
java -classpath dist/json.jar -jar dist/gameclient.jar
不太清楚为什么我仍然得到
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.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
我使用的是 Windows XP 和 Java 1.7。无论是否使用类路径选项,我都会遇到相同的错误。
最佳答案
来自java
文档:
-jar
Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your appli- cation's starting point. See the jar(1) and the Jar trail of the Java Tutorial @ http://java.sun.com/docs/books/tutorial/jar for information about working with Jar files and Jar-file manifests.
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
因此您的 gameclient.jar
必须包含您需要的所有用户类。 classpath
设置将被忽略。只需创建 gameclient.jar
以包含 json.jar
中的类即可。
关于java - 类路径不工作 - java lang ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12681359/