我使用 Maven 阴影插件来构建可执行 jar,当它在构建中包含属性时,它可以正常工作。
但是当我尝试将属性移出到单独的文件夹时,失败并显示 java.io.FileNotFoundException: class path resource [app.properties] cannot be opened because it does not exist
。
我尝试了以下三种方法,但均失败:
-
java -cp "properties"
(或".\*;properties\*"
)-jar app.jar
-
SET CLASSPATH =.\*;properties\*
- 甚至将 app.properties 放在与 app.jar 相同的路径中
正如你所看到的路径分隔符,我使用 Windows 来运行 jar。
[更新]
我更改了阴影配置(从 <mainClass>
更改为 <manifestEntries>
),如下所示,它解决了问题。
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.lifeshield.reminder.App</Main-Class>
<Class-Path>properties/</Class-Path>
<Build-Number>0.1</Build-Number>
</manifestEntries>
</transformer>
@Marko Živanović,感谢您指出-cp
和-jar
用法。
最佳答案
您不能同时使用 -cp
和 -jar
选项。
如果您使用-jar
,那么您已经在 list 文件中设置了类路径。 More details .
如果您想使用-cp
,那么您必须通过指定类名来运行应用程序。 Details .
关于java - 另一个 Java 类路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29032835/