I just want to read the file in my program. The file is located in one directory above the working directory "../f.fsh". So the following code works correctly when I run it in the IDE
String name="../f.fsh"; InputStream is = getClass().getResourceAsStream(name); InputStreamReader isreader=new InputStreamReader(is);//CRASHES HERE WITH NULL POINTER EXCEPTION BufferedReader br = new BufferedReader(isreader);
but when I create a JAR file with f.fsh, zipped inside it and starting it, it fails to create an InputStreamReader, because InputStream is null.
I read a bunch of answers to questions about input streams and JAR files, and I realized that I should use relative paths, but I already do this. As far as I understand, getResourceAsStream () can find files relative to the project root, this is what I want. Why does this not work in a JAR? What is wrong, how can I fix it?
Is this related to classpath? I thought that this was only to include files external to a working bank.
I also tried, but still fail when I put the slash:
InputStream is = getClass().getResourceAsStream("\\"+name);
I looked: How to get the resource path in a Java JAR file and found that the contents of the JAR may not necessarily be available as a file. Therefore, I tried it with copying the file in relation to the bank (one directory from the bank), and this still fails. In any case, I would like to leave my files in the bank and be able to read them there. I do not know what's happening.
source share