我将 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/