Executable Jar cannot find typesafe/config application.conf on classpath

那年仲夏 提交于 2019-12-19 09:43:07

问题


I have a command line app that downloads some reports, processes them, then uploads data to Google Drive. I'm using Typesafe-Config for all the magic strings I need. Typesafe-Config looks on the classpath for my application.conf file and uses HOCON to map config objects to fields in my class, like this:

From ~/configs/application.conf:

my.homePageUrl = "https://my.home.com"

From MyClass.java:

private static Config config = ConfigFactory.load();
private static final String HOME_URL = config.getString("my.homePageUrl");

I'm using the maven-shade-plugin to build an executable .jar for easy deployment on a remote server. The plugin looks like this:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <manifestEntries>
                                <Main-Class>com.my.reports.ReportRunner</Main-Class>
                                <Class-Path>~/configs/application.conf</Class-Path>
                            </manifestEntries>
                        </transformer>
                    </transformers>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

The problem is, when I run the executable .jar, application.conf isn't found on my classpath (I guess this also could be a bug in the typesafe code). All this works just fine in Intellij.

dustinevan@iMac:~/bin$ java -jar my-reports-1.0-SNAPSHOT.jar 
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'my'
    at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:124)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:147)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:159)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:164)
    at com.typesafe.config.impl.SimpleConfig.getList(SimpleConfig.java:212)
    at com.typesafe.config.impl.SimpleConfig.getHomogeneousUnwrappedList(SimpleConfig.java:271)
    at com.typesafe.config.impl.SimpleConfig.getStringList(SimpleConfig.java:329)
    at com.stellarliving.reports.ecp.ReportRunner.<clinit>(ReportRunner.java:19)

dustinevan@iMac:~/configs$ ls
total 8
-rw-r--r--@  1 dustinevan  staff  1520 Jun 13 01:16 application.conf

I've tried MANY permutations, and done lots of reading to solve this, any help would be greatly appreciated.


回答1:


As I just had the same problem...

The -jar overrides all classpath settings, so only the jar is seen. -Dconfig.trace=loads will show what is seen by java.

We want the application.conf on the classpath, as well as the jar, so: java -cp .:my-reports-1.0-SNAPSHOT.jar full.path.to.main.Main did the trick for me. application.conf found and overrides reference.conf in the jar.




回答2:


I also had that issue. I've noticed that you use a Class-Path declaration in shaded configuration, so I've merged the answer by Lothar with your info and added:

<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    <manifestEntries>
        <Main-Class>com.my.reports.ReportRunner</Main-Class>
            <Class-Path>.</Class-Path>
    </manifestEntries>
</transformer>


来源:https://stackoverflow.com/questions/30822551/executable-jar-cannot-find-typesafe-config-application-conf-on-classpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!