Issue with reading a file that was generated when JAR is run for second time

The “mod” folder, shown above, is generated at run-time during the second time I execute the JAR. That means, this “mod” folder does not exist in the CLASSPATH of the JAR file.

The “mod” file structure is like this:

I’ve been trying to get the “aas.script” be fed into the BufferedReader, like so:


BufferedReader reader = new BufferedReader(new InputStreamReader(Script.class.getClassLoader().getResourceAsStream(filename)));

The BufferedReader code works normally, as shown here:

Is there some other way that I am unaware of that lets me read a file generated outside of the JAR file (particularly when the JAR is in the /bin/ folder of my project root folder)?

Thanks in advance.

getResource returns a file from the classpath.

As you rightly point out, “aas.script” is not on the classpath.

Use File / FileInputStream

Yes! I totally forgot FileInputStream.

Thank you! I used a Boolean flag to separate script loading. Results:


if (isModdedScript){
				reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filename))));
			}
			else {
				reader = new BufferedReader(new InputStreamReader(Script.class.getClassLoader().getResourceAsStream(filename)));
			}