可执行 JAR 忽略其自身的 Class-Path 属性 [英] Executable JAR ignores its own Class-Path attribute

查看:24
本文介绍了可执行 JAR 忽略其自身的 Class-Path 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 Maven - 如何将任意类路径条目添加到 jar 中的说明以添加任意Class-Path 属性的条目.这是我的 MANIFEST.MF 文件:

清单版本:1.0类路径: jace-runtime.jar主类:org.jace.examples.Test

我定义 org.jace.examples.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.ShutdownHookjace-runtime.jar 中定义.当我调用 java -jar peer_example1.jar 时,我得到以下输出:

类路径:peer_example1.jar线程main"中的异常 java.lang.ClassNotFoundException: org.jace.util.ShutdownHook

换句话说,Java 将可执行 JAR 文件添加到类路径,但忽略了 Class-Path.如果我调用 java -cp jace-runtime.jar;peer_example1.jar org.jace.examples.Test 我得到预期的输出:

classpath: jace-runtime.jar;peer_example1.jar

有什么想法吗?

解决方案

回答我自己的问题:

  1. Class-Path 添加任意条目本身就没有问题.使用以下方法启用 JAR 索引时会出现问题:

    <预><代码><配置><存档><index>true</index></归档></配置>

    Maven 将从 META-INF/INDEX.LIST 中省略您的输入.

  2. 当您在运行时使用 JAR 时,Java 将查看 INDEX.LIST 来查找类,而不是 MANIFEST.MF.

  3. 因为 INDEX.LIST 中缺少您的条目,所以无论清单说什么,类加载器都不会找到它们.

一个简单的解决方法是禁用 JAR 索引.我不确定如何在启用索引的情况下注入任意 Class-Path.

I used the instructions found at Maven - how can I add an arbitrary classpath entry to a jar 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

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"));
    }
}

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 Exception in thread "main" java.lang.ClassNotFoundException: org.jace.util.ShutdownHook

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

Any ideas?

解决方案

Answering my own question:

  1. 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 will omit your entries from META-INF/INDEX.LIST.

  2. When you use the JAR at runtime, Java will look at INDEX.LIST for finding classes, not MANIFEST.MF.

  3. Because your entries are missing from INDEX.LIST, they will not be found by the classloader, no matter what the manifest says.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆