简体   繁体   English

Java忽略jar文件清单中的Class-Path条目

[英]Java ignoring Class-Path entry in jar file's manifest

I have searched the internet and I have searched stackoverflow and found many many posts asking this question, yet there isn't ever a concrete answer. 我已经搜索了互联网,我搜索了stackoverflow,发现很多帖子都在问这个问题,但是没有一个具体的答案。


The short version is: I have a jar file which references other jar files in the manifest. 简短的版本是:我有一个jar文件,它引用了清单中的其他jar文件。 I then run the jar file with java -jar test.jar, but it doesn't add the jar dependencies to the classpath. 然后我使用java -jar test.jar运行jar文件,但它不会将jar依赖项添加到类路径中。


Long version: I made a little experiment in order to confirm that it wasn't something special about my project. 长版本:我做了一个小实验,以确认它对我的项目不是特别的。 I created a simple Java Project in eclipse, with only 1 class. 我在eclipse中创建了一个简单的Java项目,只有一个类。 All this class does is write the classpath to std-out: 这个类所做的就是将类路径写入std-out:

package test;

import java.net.URL;
import java.net.URLClassLoader;

public class Test {
  public static void main(String[] args){
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    URL[] urls = ((URLClassLoader)cl).getURLs();
    for(URL url: urls){
      System.out.println("CP: "+url.getFile());
    }
  }
}

Then I created a "lib" folder and put a random jar file inside and added it to the classpath. 然后我创建了一个“lib”文件夹并在其中放入一个随机的jar文件并将其添加到类路径中。 The folder structure now looks like this: 文件夹结构现在看起来像这样:

Test
|- src/test/Test.java
|- lib/mysql-connector-java-3.1.14-bin.jar

Now when I run it from Eclipse I get the following output: 现在,当我从Eclipse运行它时,我得到以下输出:

CP: /home/username/workspace/Test/bin/
CP: /home/username/workspace/Test/lib/mysql-connector-java-3.1.14-bin.jar

Ok, now I use Eclipse to export the project to a runnable jar. 好的,现在我使用Eclipse将项目导出到一个可运行的jar。 I choose the option "Copy required libraries into sub-folder next to the generated jar". 我选择“将所需库复制到生成的jar旁边的子文件夹”选项。

This creates the following structure: 这将创建以下结构:

/tmp/Test
|- test.jar
|- test_lib/mysql-connector-java-3.1.14-bin.jar

If I look into the Test.jar that was created by Eclipse, it looks like this: 如果我查看Eclipse创建的Test.jar,它看起来像这样:

test.jar
|- test/Test.class
|- META-INF/MANIFEST.MF

The contents of the MANIFEST.MF are: MANIFEST.MF的内容是:

Manifest-Version: 1.0
Class-Path: . test_lib/mysql-connector-java-3.1.14-bin.jar
Main-Class: test.Test

And yes, there is an empty line after the Main-Class attribute (I mention this because some people posted online that the cause could be a missing newline at the end). 是的,在Main-Class属性之后有一个空行(我提到这是因为有些人在网上发帖说原因可能是最后一个缺少的换行符)。

So, everything looks fine. 所以,一切看起来都很好。 I run it with java -jar test.jar and get the following output: 我用java -jar test.jar运行它并得到以下输出:

CP: /tmp/Test/test.jar

So the classpath contains only the jar file that I want to run . 所以classpath 包含我想要运行的jar文件 The Class-Path: attribute in the MANIFEST is completely ignored. MANIFEST中的Class-Path:属性被完全忽略。 The same thing happens if I run it with java -cp test.jar test.Test 如果我用java -cp test.jar test.Test运行它会发生同样的事情

How does one use the Class-Path attribute in the MANIFEST.MF properly? 如何正确使用MANIFEST.MF中的Class-Path属性? I can't figure it out for the life of me. 我无法理解我的生活。 Especially since -cp and -jar are not compatible, I don't know how I could specify the classpath while also using the -jar argument. 特别是因为-cp和-jar不兼容,我不知道如何在使用-jar参数的同时指定类路径。

Thanks a lot in advance. 非常感谢提前。


A few more things I can answer in advance: 还有一些我可以提前回答的事情:

  • "Is there a line break after the last line?" “在最后一行之后是否有换行符?” - Yes - 是的
  • "Could it be eclipse malforms the MANIFEST.MF?" “这可能是eclipse对MANIFEST.MF的错误吗?” - Could be, but I have the same problem when I make the jar with ant. - 可能是,但是当我用蚂蚁制作罐子时,我遇到了同样的问题。 So if both eclipse and ant malform the manifest, I don't know how I should format it. 因此,如果eclipse和ant都会使清单变形,我不知道应该如何格式化它。
  • "What platforms, Java versions have you tried?" “你试过哪些平台,Java版本?” - I tried Java8 on Windows 10, as well as Java6, Java7 and Java8 on Fedora Linux. - 我在Windows 10上尝试过Java8,在Fedora Linux上尝试过Java6,Java7和Java8。

Ok, thanks to JB Nizet's comment I figured out that everything is working fine. 好的,感谢JB Nizet的评论,我发现一切都运转正常。 It doesn't print out the classpath but I can access the classes from the library. 它不会打印出类路径,但我可以从库中访问这些类。 So my experiment was set up wrong. 所以我的实验设置错了。 This is bad news for me, as my other project still doesn't work, and it is too complex to post here. 这对我来说是个坏消息,因为我的其他项目仍然不起作用,而且在这里发布太复杂了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM