Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload