Get resource from jar
27,011
Solution 1
To retrieve the file inside the jar, use:
private static final String ENDINGS_FILE_NAME = "/res/endingRule.txt";
...
InputStream is = getClass( ).getResourceAsStream(ENDINGS_FILE_NAME);
Solution 2
Your name looks wrong - incorrect extension and the wrong kind of slash:
private static final String ENDINGS_FILE_NAME = "res/endingRule.txt";
Solution 3
The \
is being interpreted as an escape character rather than directory separator. File name is also off. Try:
private static final String ENDINGS_FILE_NAME = "res/endingRule.txt";
Author by
Victoria Seniuk
Updated on June 18, 2020Comments
-
Victoria Seniuk almost 4 years
I have jar with files:
myJar/res/endingRule.txt myJar/wordcalculator/merger/Marge.class
In Marge.java I have code:
But after this inputStream is null. How to work with resources? Why null?private static final String ENDINGS_FILE_NAME = "res/endingRule.txt"; .... InputStream inputStream = getClass().getClassLoader().getResourceAsStream(ENDINGS_FILE_NAME); .....