Simple Java program:
public static String loadText(String file) {
StringBuilder finalString = new StringBuilder();
InputStream in = null;
BufferedReader reader = null;
InputStreamReader isr = null;
try{
System.out.println("Text File: " + file);
// Version 1
//URL url = Thread.currentThread().getClass().getResource(file);
//in = url.openStream();
// Version 2
in = Class.class.getResourceAsStream(file);
isr = new InputStreamReader(in);
reader = new BufferedReader(isr);
String line;
while((line = reader.readLine()) != null) {
finalString.append(line).append("//\n");
}
}
catch(IOException e) {
e.printStackTrace();
System.exit(-1);
}
finally {
try {
if (isr != null) { isr.close(); }
if (reader != null) { reader.close(); }
if (in != null) { in.close(); }
} catch (IOException e) { e.printStackTrace(); }
}
return finalString.toString();
}
The getResource
and getResourceAsStream
methods works fine in JDK 8 (java-8-openjdk-amd64) but they always return null
in JDK 11.
Questions: Why? And how can I fix this?
- Operation System: Linux Mint 19 Tara x64
- IDE: Eclipse 2018-12 (4.10.0)