Cuurent state As chris suggested i created a minimum example (maven based):
public class MainApp {
public static void main(String[] args) {
System.out.println(MainApp.class.getResourceAsStream("/my.properties"));
System.out.println(MainApp.class.getClassLoader().getResourceAsStream("their.properties"));
}
}
This is working (same maven config as in my not working app, and same MANIFEST.MF in same location )! So i just copied the 2 lines from above into my real app, built it and copied the 2 files into jar's folder. but now both sysouts yield "null"! So something seems to fiddling around with classpath/ or let ignore classpath from manifest? (the output from printing out class path is same in both cases - only the jar is printed out) Any idea how to nail down the problem?
original question:
I have problem to read a file (properties) from current directory. Here is my setup (maven based): reading of file:
MyClass.class.getClassLoader().getResourceAsStream("my.properties")
Works fine within tests an my properties placed in resources/my.properties.
Since the current directory is the default classpath i thought it should just work to put my.properties in same directory as jar and run java -jar
from this directory, but it didn't work (resource stream is null).
Then i added odd code to main to print out class path:
String classpath = System.getProperty("java.class.path");
String[] classpathEntries = classpath.split(File.pathSeparator);
for (int i = 0; i < classpathEntries.length; i++) {
System.out.println(classpathEntries[i]);
}
I also tried some other code but in both cases only my jar file's path was printed out. So i tried to add '.' to Manifest by using maven assembly plugin. From manifest:
...
Class-Path: .
...
But this didn't helped. So how to get my program load getClassLoader().getResourceAsStream(my.properties)
from current directory?
Why this is not a duplicate of this I have no problem to load the resource from classpath. i put the file in src/test/resource/my.properties Maven copies them together with all classes and all is working fine. Since the file is in src/test/resources it is missing in packaged jar (as intended). The user must give a my.properties to run the program. And i want to read this file from current directory (assuming that java - jar is called from directory with jar an my.properties) but this isn't working!
EDIT maven-assembly-plugin config:
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>some.pkg.MainApp</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
This results in manifest ...jar/META-INF/MANIFEST.MF