java - 为什么从 CMD 运行 JAR 文件时收到 NoClassDefFoundError?

标签 java jar cmd

我将 jar 文件放置在 C:\temp 下

jar list 文件内包含所有相关的 jar 文件都在 lib 文件夹下

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.1
Created-By: 1.8.0_111-b14 (Oracle Corporation)
Class-Path: lib/httpclient-4.2.1.jar lib/httpcore-4.2.1.jar lib/common s-logging-1.1.1.jar lib/httpclient-cache-4.2.5.jar lib/httpmime-4.2.5.jar lib/log4j-1.2.13.jar lib/log4j-api-2.0-beta9.jar lib/log4j-core-2.0-beta9.jar lib/gson-2.2.4-javadoc.jar lib/gson-2.2.4-sources.jar lib/gson-2.2.4.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: abc.service.Main

当我执行时

java -jar C:\temp\test.jar

我收到以下错误:

A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient

最佳答案

您假设Class-Path引用jar文件内的jar文件。事实并非如此。来自 "Adding classes to the JAR file's classpath" :

Note: The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over Internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.

因此,将 jar 文件解压到主 jar 文件旁边。

关于java - 为什么从 CMD 运行 JAR 文件时收到 NoClassDefFoundError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41184019/

相关文章:

java - 在 Eclipse VS 中运行 Java 程序。 CMD SSL 证书

java - 如何设置 JDI 启动连接器?

java - log4J 序列日志号

java - 模拟 RCP 应用程序的 jar 文件。哪些因素可能会导致其出错?

java - 类路径在哪里?

java - 在我的 GUI (java) 中显示 CMD 输出

java - 没有适合 postgres 的驱动程序,即使 Class.forName 有效?

java - 如何从设备读取和写入超大数据

android - jar 绑定(bind) zubhium sdk 的 Monodroid 问题

windows - 合并时批处理文件不会运行(在批处理文件中退出 ghostscript