可执行JAR忽略其自己的Class-Path属性 [英] Executable JAR ignores its own Class-Path attribute
问题描述
我使用 https://stackoverflow.com/a/5893391/14731 上的说明添加任意内容进入 Class-Path
属性。这是我的MANIFEST.MF文件:
I used the instructions found at https://stackoverflow.com/a/5893391/14731 to add an arbitrary entry to the Class-Path
attribute. Here is my MANIFEST.MF file:
Manifest-Version: 1.0
Class-Path: jace-runtime.jar
Main-Class: org.jace.examples.Test
我定义了org.jace。 examples.Test如下:
I defined org.jace.examples.Test as follows:
public class Test
{
public static void main(String[] args)
{
System.out.println("classpath: " + System.getProperty("java.class.path"));
System.out.println("PeerExample: " + Class.forName("org.jace.util.ShutdownHook"));
}
}
其中 org.jace。 util.ShutdownHook
在 jace-runtime.jar
中定义。当我调用 java -jar peer_example1.jar
时,我得到以下输出:
where org.jace.util.ShutdownHook
is defined in jace-runtime.jar
. When I invoke java -jar peer_example1.jar
I get the following output:
classpath :peer_example1.jar
线程main中的异常java.lang.ClassNotFoundException:org.jace.util.ShutdownHook
classpath: peer_example1.jar
Exception in thread "main" java.lang.ClassNotFoundException: org.jace.util.ShutdownHook
In换句话说,Java正在将可执行JAR文件添加到类路径中,但忽略 Class-Path
。如果我调用 java -cp jace-runtime.jar; peer_example1.jar org.jace.examples.Test
我得到了预期的输出:
In other words, Java is adding the executable JAR file to the classpath but ignoring Class-Path
. If I invoke java -cp jace-runtime.jar;peer_example1.jar org.jace.examples.Test
I get the expected output:
classpath:jace-runtime.jar; peer_example1.jar
任何想法?
推荐答案
回答我自己的问题:
-
将任意条目添加到
Class-Path
本身就可以了。使用以下方法启用JAR索引时会出现问题:
Adding arbitrary entries to
Class-Path
is fine on its own. The problem arises when you enable JAR indexing using:
<configuration>
<archive>
<index>true</index>
</archive>
</configuration>
Maven将从 META-INF / INDEX.LIST中省略您的参赛作品
。
在运行时使用JAR时,Java会查看INDEX.LIST以查找类,而不是MANIFEST.MF。
When you use the JAR at runtime, Java will look at INDEX.LIST for finding classes, not MANIFEST.MF.
因为INDEX.LIST中缺少您的条目,所以无论清单显示什么,类加载器都不会找到它们。
Because your entries are missing from INDEX.LIST, they will not be found by the classloader, no matter what the manifest says.
一个简单的解决方法是禁用JAR索引。我不确定如何在启用索引的情况下注入任意 Class-Path
。
A simple workaround is to disable JAR indexing. I'm not sure how to inject an arbitrary Class-Path
with indexing enabled.
这篇关于可执行JAR忽略其自己的Class-Path属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!