可执行的jar将找不到属性文件 [英] Executable jar won't find the properties files

查看:102
本文介绍了可执行的jar将找不到属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 属性属性= new Properties(); 
URL url = new App()。getClass()。getResource(PROPERTIES_FILE);
properties.load(url.openStream());

代码在Eclipse中运行正常。然后我将程序打包成一个名为MyProgram.jar的JAR,并运行它,第二行有一个NullPointerException。 JAR不包含属性文件,它们都在同一目录中。我使用Maven创建JAR。如何解决这个问题?



更新:我不想将属性文件添加到JAR,因为它将在部署时创建。 >

解决方案

BalusC是对的,你需要指示Maven生成一个 MANIFEST.MF Class-Path中的当前目录(



假设您仍然使用Maven程序集插件和 jar-with-dependencies 描述符来构建可执行JAR,可以告诉插件做所以使用以下内容:

 < plugin> 
< artifactId> maven-assembly-plugin< / artifactId>
< version> 2.2< / version>
< configuration>
< descriptorRefs>
< descriptorRef> jar-with-dependencies< / descriptorRef>
< / descriptorRefs>
< archive>
< manifest>
< mainClass> com.stackoverflow.App< / mainClass>
< / manifest>
< manifestEntries>
< Class-Path>。< / Class-Path> <! - 这里是重要的位 - >
< / manifestEntries>
< / archive>
< / configuration>
<执行>
< execution>
< id> make-assembly< / id> <! - 这用于继承合并 - >
< phase> package< / phase> <! - 追加到包装阶段。 - >
< goals>
< goal> single< / goal> <! - goals == mojos - >
< / goals>
< / execution>
< / executions>
< / plugin>


I use this code in my program to load a properties file:

Properties properties = new Properties();
URL url = new App().getClass().getResource(PROPERTIES_FILE);
properties.load(url.openStream());

The code runs fine in Eclipse. Then I package the program into a JAR named MyProgram.jar, and run it, I got a NullPointerException at the second line. The JAR doesn't contain the properties file, they both are in the same directory. I am using Maven to create the JAR. How can I fix this problem?

UPDATE: I don't want to add the properties file to the JAR, since it will be created at deployment time.

解决方案

BalusC is right, you need to instruct Maven to generate a MANIFEST.MF with the current directory (.) in the Class-Path: entry.

Assuming you're still using the Maven Assembly Plugin and the jar-with-dependencies descriptor to build your executable JAR, you can tell the plugin to do so using the following:

  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.stackoverflow.App</mainClass>
        </manifest>
        <manifestEntries>
          <Class-Path>.</Class-Path> <!-- HERE IS THE IMPORTANT BIT -->
        </manifestEntries>
      </archive>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- append to the packaging phase. -->
        <goals>
          <goal>single</goal> <!-- goals == mojos -->
        </goals>
      </execution>
    </executions>
  </plugin>

这篇关于可执行的jar将找不到属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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