无法在JAR中加载属性文件

时间:2018-09-19 12:32:26

标签: java maven jar

可执行jar在最顶层包含一个文本文件(jdbc.properties):

$ unzip -l target/logic-jar-with-dependencies.jar | grep jdbc.properties
       97  09-19-2018 14:57   jdbc.properties

但是,当我运行它时:

$ java -jar target/logic-jar-with-dependencies.jar

我明白了

Exception in thread "main" java.lang.NullPointerException
        at java.util.Properties$LineReader.readLine(Unknown Source)
        at java.util.Properties.load0(Unknown Source)
        at java.util.Properties.load(Unknown Source)
...

下面的代码中引发了异常:

...
Properties prop = new Properties();
prop.load(getClass().getResourceAsStream("jdbc.properties"));
...

但是当我将第二行更改为:

prop.load(new FileInputStream("jdbc.properties"));

并运行:

$  mvn exec:java -Dexec.mainClass=...

有效。 JAR包装不正确还是什么?

0 个答案:

没有答案