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

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

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 am using maven-shade-plugin

to create a .jar executable for easy deployment to 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 that when I run the executable .jar is application.conf

not found in my classpath (I think this could also be a bug in the typafe code). This all works great 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 a lot of permutations and read a lot to solve this problem, any help would be greatly appreciated.

+3


source to share


2 answers


Once I had the same problem ...

-jar

overrides all classpath parameters, so only the jar is displayed. -Dconfig.trace=loads

will show what is visible java.



We want the application.conf on the classpath as well as in 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.

+3


source


I also had this question. I noticed that you are using a declaration Class-Path

in a shaded configuration, so I combined Lothar's answer with your information 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>

      

+1


source







All Articles