I have jar with files:
myJar/res/endingRule.txt
myJar/wordcalculator/merger/Marge.class
In Marge.java I have code:
private static final String ENDINGS_FILE_NAME = "res/endingRule.txt";
....
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(ENDINGS_FILE_NAME);
.....
But after this inputStream is null. How to work with resources? Why null?
Jar files are archive files that contains of a lot of different java classes (files). You can use winzip/winrar to open the jar files and you can see those java classes in jar files. Typically you can use a Java decompiler to decompile the class file and look into the source code.
jar ) contain your executable classes and resource files. A jar can also contain other jar files, which is useful when your program needs some library which is packaged in a jar.
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);
Your name looks wrong - incorrect extension and the wrong kind of slash:
private static final String ENDINGS_FILE_NAME = "res/endingRule.txt";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With