I have something like the following project structure
-project
-src/main/java // source folder
-com.mypackage
-Utility.java
-some.properties
-src/main/resources // source folder
-configuration_files
-configuration_report.txt
I'm generating the jar
with Eclipse's Export
function. The above translates to a jar
with the structure:
-myjar.jar
-com
-mypackage
-Utility.class
-some.properties
-configuration_files
-configuration_report.txt
Within the Utility
class, I'm trying to get the InputStream
from configuration_report.txt
.
I was doing this (it's within a static
method):
Utility.class.getResourceAsStream("/configuration_files/configuration_report.txt");
which seems to be validated by the answer to this question, since the file is not in the same package as the class requesting it.
When I run it, however, it returns null
. Why?
Note that for the some.properties
file, I could do both
Utility.class.getResourceAsStream("/com/package/some.properties");
Utility.class.getResourceAsStream("some.properties");
and I would get the stream.
I'm running this from the command line if it makes any difference.
java [-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=1044] -cp myjar.jar MyMain
If I try to run it within Eclipse, it works...
Edit:
This code is relevant. With or without the preceding /
, it return null
String sourceFilePath = "/configuration_files" + File.separator + "configuration_report.txt";
InputStream in = Utility.class.getResourceAsStream(sourceFilePath);