How to reference property files in AND Eclipse executable jar?

I use two properties files: log4j.properties and myapp.properties . I want to download them correctly when I run my application in Eclipse AND in an executable jar.

ATMs are stored here: /src/configs/*.properties . I refer to them in my code using this line:

 config = new PropertiesConfiguration(getClass().getResource("/configs/myapp.properties")); 

This works fine if I run my application in Eclipse, but fails if I execute the executable jar file (from the generated eclipse). I created a manifest file in /META-INF/ and entered this line in it:

 Class-Path: . 

To execute, executing the jar still fails :-( Where do I need to put my property files and how do I reference them?

Can I also reference them outside of the jar if I execute the jar and inside my project if I am in Eclipse? Thanks!

+4
source share
2 answers

The way you are trying to load a properties file looks great. Have you checked if the property files are really part of the generated jar file?

+1
source

Typically, property files are intended to be deployed / modified without rebuilding the jar, for example. environment / external resources, so they are stored separately.

But if you need to keep them inside, make sure that (in your case) "configs / myapp.properties" is at the root JAR server level. Just open this jar that you created to see what's there. Or you can run:

 jar -tvf your.jar | grep myapp.properties 

to see the actual path in the JAR without sharing it.

If it is, you can load them through the classloader as follows:

 ClassLoader cl = YourClass.class.getClassLoader() config = new PropertiesConfiguration( cl.getResourceAsStream( "configs/myapp.properties" ) ) ); 
0
source

All Articles