简体   繁体   English

另一个Java类路径问题

[英]Another Java classpath issue

I used Maven shade plugin to build the executable jar, which works fine when it include the properties in the build. 我使用Maven shade插件来构建可执行jar,当它在构建中包含属性时,它可以很好地工作。

But when I tried to move properties out to a separate folder, it failed with java.io.FileNotFoundException: class path resource [app.properties] cannot be opened because it does not exist . 但是,当我尝试将属性移到单独的文件夹时,它失败并出现java.io.FileNotFoundException: class path resource [app.properties] cannot be opened because it does not exist

I have tried the following three methods, but all failed: 我尝试了以下三种方法,但都失败了:

  1. java -cp "properties" (or ".\\*;properties\\*" ) -jar app.jar java -cp "properties" (或".\\*;properties\\*" )- -jar app.jar
  2. SET CLASSPATH =.\\*;properties\\*
  3. or even put app.properties in the same path as app.jar 甚至将app.properties放在与app.jar相同的路径中

As you can see the path separator, I used Windows to run the jar. 如您所见,路径分隔符是使用Windows运行jar的。

[update] [更新]

I changed the shade config (from only <mainClass> to <manifestEntries> ) as below, and it resolved the issue. 我如下更改了阴影配置(仅从<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ć, thanks for pointing out the -cp and -jar usage. @MarkoŽivanović,感谢您指出-cp-jar用法。

You cannot use both -cp and -jar options. 您不能同时使用-cp-jar选项。

If you use -jar then you have set classpath in manifest file. 如果使用-jar则在清单文件中设置类路径。 More details . 更多细节

If you want to use -cp then you have to run application by specifying class name. 如果要使用-cp则必须通过指定类名来运行应用程序。 Details . 细节

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

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