我的jar文件中的目录是这样的:
在remote.jar
-com
-lib
-META-INF
在META-INF
下是文件MANIFEST.MF
:
Manifest-Version: 1.0
Class-Path: lib/*
Main-Class: com.xx.xx.Main
所有的jar都在lib
文件夹下。
当我运行 java -jar remote.jar
时,从命令行给出了 ClassNotFoundException
,未找到的类位于 lib 下的一个 jar 中
文件夹。如果 main
函数没有引用 lib 中的任何类,则命令行运行没有问题。
我的问题是为什么在lib下的jar中找不到类。
编辑:
文件夹 lib
下没有嵌套文件夹
最佳答案
问题是 list 中的 Class-Path 属性并不像您想象的那样起作用。参见 http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
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 之外,并创建一个小的批处理文件或 shell 脚本来构建类路径并调用你的 jar。
关于java - 运行可执行 jar 抛出 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19831979/