java - 类路径不工作 - java lang ClassNotFoundException

标签 java classpath

我有一个名为 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/

相关文章:

java - getClass().getClassLoader().getResourceAsStream 抛出 NullPointerException

读取文件时发生 Java EOFException

java - 如何将 netty ByteBuf 转换为 String,反之亦然

java - 在非嵌入式 Tomcat 上使用 SSL 运行 SpringBoot

java - LOGBACK:将日志写入类路径中的文件

java - 尽管设置了 PATH 和 CLASSPATH,但无法执行 jar 文件

Java类路径问题

java - 当更新项目时我不希望 src/test/java 出现在 Eclipse java 构建路径源类别中时,如何配置 maven pom.xml?

java - 每月复利 Java 应用程序

windows-7 - 找不到 javac - 在 Windows 7 64 位上为 JDK 设置了 CLASSPATH/Path/JAVA_HOME