Executable jar won't find the properties files
BalusC is right, you need to instruct Maven to generate a MANIFEST.MF
with the current directory (.
) in the Class-Path:
entry.
Assuming you're still using the Maven Assembly Plugin and the jar-with-dependencies
descriptor to build your executable JAR, you can tell the plugin to do so using the following:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.stackoverflow.App</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path> <!-- HERE IS THE IMPORTANT BIT -->
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- append to the packaging phase. -->
<goals>
<goal>single</goal> <!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
There are two workarounds:
Don't use the JAR as executabele JAR, but as library.
java -cp .;filename.jar com.example.YourClassWithMain
Obtain the root location of the JAR file and get the properties file from it.
URL root = getClass().getProtectionDomain().getCodeSource().getLocation(); URL propertiesFile = new URL(root, "filename.properties"); Properties properties = new Properties(); properties.load(propertiesFile.openStream());
None of both are recommended approaches! The recommend approach is to have the following entry in JAR's /META-INF/MANIFEST.MF
file:
Class-Path: .
Then it'll be available as classpath resource the usual way. You'll really have to instruct Maven somehow to generate the MANIFEST.MF
file like that.