简体   繁体   English

从Eclipse导出的JAR文件与可运行的JAR文件中的行为不同

[英]Different behavior in JAR file vs Runnable JAR file exported from Eclipse

I'm getting different results from the code below depending upon whether I select JAR file or Runnable JAR file from Eclipse's File|Export dialog: 根据从Eclipse的File | Export对话框中选择JAR文件还是Runnable JAR文件 ,我从下面的代码中得到不同的结果:

public static void main(String[] args) throws IOException {
    Enumeration<URL> dirs = Thread.currentThread().getContextClassLoader().getResources(
                "com/test/pojo");
    System.out.println(dirs.hasMoreElements());
}

If I choose: 如果我选择:

  • JAR file - the code above prints false JAR文件 -上面的代码显示为false
  • Runnable JAR file - the code above prints true 可运行的JAR文件 -上面的代码显示为true

I compared the contents of JARs exported both ways, and the files in them are exactly the same! 我比较了两种方式导出的JAR的内容,它们中的文件完全相同!

Can anyone explain what's going on here? 谁能解释这是怎么回事?

I'm not sure of my answer, but I'll try : your files are maybe the same, but it's doesn't imply that the meta-data of the files are the same, ie you can have to different files (eg in different format) but containing the same data. 我不确定我的答案,但我会尝试:您的文件也许是相同的,但这并不意味着文件的元数据是相同的,即您可以拥有不同的文件(例如,不同的格式),但包含相同的数据。 So maybe the meta-data of Runnable JAR and JAR are different (and it wouldn't be surprising). 因此,也许Runnable JAR和JAR的元数据是不同的(这并不奇怪)。

Problem solved!ClassLoader.getResources() will load the classes in my jar, but none-runnable jar didn't maintain the correct directory enties. 问题已解决!ClassLoader.getResources()将类加载到我的jar中,但是不可运行的jar无法维护正确的目录实体。

Runnable-JAR: 可运行的JAR:

jar tf hall1.jar
META-INF/MANIFEST.MF
  com/
  com/test/
  com/test/Main.class
  com/test/pojo/
  com/test/pojo/Pojo.class

None-Runnable JAR: 不可运行的JAR:

jar tf hall1.jar
  META-INF/MANIFEST.MF
  com/test/Main.class
  com/test/pojo/Pojo.class

So all I have to do is selecting "Options-Add directory entries" in JAR-Export pannel 因此,我要做的就是在JAR-Export面板中选择“选项-添加目录条目”

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

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