Why does the getResource() method return null in JDK 11?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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)
java java-11
add a comment |
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)
java java-11
What's a typical value for the 'file' argument you are using?
– DodgyCodeException
Jan 3 at 12:04
Please check this question out
– caco3
Jan 3 at 12:05
4
Class.class.getResourceAsStream(...) will attempt to locate the resource in the java.base module. Replace "Class.class" with the name of your class so that it finds it in your module (or on the class path).
– Alan Bateman
Jan 3 at 12:48
Further, it’s worth learning about the try-with-resources statement…
– Holger
Jan 10 at 15:29
add a comment |
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)
java java-11
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)
java java-11
java java-11
edited Jan 3 at 12:56
Lii
7,25144163
7,25144163
asked Jan 3 at 11:52
AlexeyAlexey
133
133
What's a typical value for the 'file' argument you are using?
– DodgyCodeException
Jan 3 at 12:04
Please check this question out
– caco3
Jan 3 at 12:05
4
Class.class.getResourceAsStream(...) will attempt to locate the resource in the java.base module. Replace "Class.class" with the name of your class so that it finds it in your module (or on the class path).
– Alan Bateman
Jan 3 at 12:48
Further, it’s worth learning about the try-with-resources statement…
– Holger
Jan 10 at 15:29
add a comment |
What's a typical value for the 'file' argument you are using?
– DodgyCodeException
Jan 3 at 12:04
Please check this question out
– caco3
Jan 3 at 12:05
4
Class.class.getResourceAsStream(...) will attempt to locate the resource in the java.base module. Replace "Class.class" with the name of your class so that it finds it in your module (or on the class path).
– Alan Bateman
Jan 3 at 12:48
Further, it’s worth learning about the try-with-resources statement…
– Holger
Jan 10 at 15:29
What's a typical value for the 'file' argument you are using?
– DodgyCodeException
Jan 3 at 12:04
What's a typical value for the 'file' argument you are using?
– DodgyCodeException
Jan 3 at 12:04
Please check this question out
– caco3
Jan 3 at 12:05
Please check this question out
– caco3
Jan 3 at 12:05
4
4
Class.class.getResourceAsStream(...) will attempt to locate the resource in the java.base module. Replace "Class.class" with the name of your class so that it finds it in your module (or on the class path).
– Alan Bateman
Jan 3 at 12:48
Class.class.getResourceAsStream(...) will attempt to locate the resource in the java.base module. Replace "Class.class" with the name of your class so that it finds it in your module (or on the class path).
– Alan Bateman
Jan 3 at 12:48
Further, it’s worth learning about the try-with-resources statement…
– Holger
Jan 10 at 15:29
Further, it’s worth learning about the try-with-resources statement…
– Holger
Jan 10 at 15:29
add a comment |
1 Answer
1
active
oldest
votes
I've tried your application with both openjdk 8 and 11 on MacOS and it does not work with both. I think you need to look at [1] and [2] in order to understand how getResourceAsStream
works.
TLDR:
If the path is absolute (i.e. starts with a slash -
/
), thenclass.getResourceAsStream()
searches in the provided pathIf the path is NOT absolute (i.e. does not start with a slash) , then
class.getResourceAsStream()
searches in a constructed path that corresponds to the package name, where the dots are replaced with slashes
So whether it works or not depends on 2 things:
- Is your path absolute or not ?
- Is the
file
located in the same package as the class or not ?
Basically in your exaple as is provided, it can never work if the path is not absolute, because Class.class.getResourceAsStream()
will always resolve the path to java/lang/<file>
, so your file must be in a system package. So instead you must use <MyClass>.class.getResourceAsStream()
or alternatively use an absolute path
[1] https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)
[2] https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29
Update
Since Java SE 9, invoking getResourceXXX on a class in a named module will only locate the resource in that module, it will not search the class path as it did in previous release. So when you use Class.class.getResourceAsStream()
it will attempt to locate the resource in module containing java.lang.Class
, which is the java.base
module. Obviously your resource is not in that module, so it returns null.
You have to make java 9+ search for the file in your module, which most probably is an "unnamed module". You can do that by changing Class
to any class defined in your module in order to make java use the proper class loader.
I'm sorry if I am confused you. Yes, the path is absolute. TXT file in the package folder with Java class files and included in the Build Path. File Path: /configuration/options.txt
– Alexey
Jan 3 at 12:39
Ok, then it's because of the java modules. I'll update my answer
– Svetlin Zarev
Jan 3 at 12:41
Thanks for explanation. Changing from Class.class.getResourceAsStream(file) to TextTools.class.getResourceAsStream(file) solve Error.
– Alexey
Jan 3 at 12:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021754%2fwhy-does-the-getresource-method-return-null-in-jdk-11%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've tried your application with both openjdk 8 and 11 on MacOS and it does not work with both. I think you need to look at [1] and [2] in order to understand how getResourceAsStream
works.
TLDR:
If the path is absolute (i.e. starts with a slash -
/
), thenclass.getResourceAsStream()
searches in the provided pathIf the path is NOT absolute (i.e. does not start with a slash) , then
class.getResourceAsStream()
searches in a constructed path that corresponds to the package name, where the dots are replaced with slashes
So whether it works or not depends on 2 things:
- Is your path absolute or not ?
- Is the
file
located in the same package as the class or not ?
Basically in your exaple as is provided, it can never work if the path is not absolute, because Class.class.getResourceAsStream()
will always resolve the path to java/lang/<file>
, so your file must be in a system package. So instead you must use <MyClass>.class.getResourceAsStream()
or alternatively use an absolute path
[1] https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)
[2] https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29
Update
Since Java SE 9, invoking getResourceXXX on a class in a named module will only locate the resource in that module, it will not search the class path as it did in previous release. So when you use Class.class.getResourceAsStream()
it will attempt to locate the resource in module containing java.lang.Class
, which is the java.base
module. Obviously your resource is not in that module, so it returns null.
You have to make java 9+ search for the file in your module, which most probably is an "unnamed module". You can do that by changing Class
to any class defined in your module in order to make java use the proper class loader.
I'm sorry if I am confused you. Yes, the path is absolute. TXT file in the package folder with Java class files and included in the Build Path. File Path: /configuration/options.txt
– Alexey
Jan 3 at 12:39
Ok, then it's because of the java modules. I'll update my answer
– Svetlin Zarev
Jan 3 at 12:41
Thanks for explanation. Changing from Class.class.getResourceAsStream(file) to TextTools.class.getResourceAsStream(file) solve Error.
– Alexey
Jan 3 at 12:59
add a comment |
I've tried your application with both openjdk 8 and 11 on MacOS and it does not work with both. I think you need to look at [1] and [2] in order to understand how getResourceAsStream
works.
TLDR:
If the path is absolute (i.e. starts with a slash -
/
), thenclass.getResourceAsStream()
searches in the provided pathIf the path is NOT absolute (i.e. does not start with a slash) , then
class.getResourceAsStream()
searches in a constructed path that corresponds to the package name, where the dots are replaced with slashes
So whether it works or not depends on 2 things:
- Is your path absolute or not ?
- Is the
file
located in the same package as the class or not ?
Basically in your exaple as is provided, it can never work if the path is not absolute, because Class.class.getResourceAsStream()
will always resolve the path to java/lang/<file>
, so your file must be in a system package. So instead you must use <MyClass>.class.getResourceAsStream()
or alternatively use an absolute path
[1] https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)
[2] https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29
Update
Since Java SE 9, invoking getResourceXXX on a class in a named module will only locate the resource in that module, it will not search the class path as it did in previous release. So when you use Class.class.getResourceAsStream()
it will attempt to locate the resource in module containing java.lang.Class
, which is the java.base
module. Obviously your resource is not in that module, so it returns null.
You have to make java 9+ search for the file in your module, which most probably is an "unnamed module". You can do that by changing Class
to any class defined in your module in order to make java use the proper class loader.
I'm sorry if I am confused you. Yes, the path is absolute. TXT file in the package folder with Java class files and included in the Build Path. File Path: /configuration/options.txt
– Alexey
Jan 3 at 12:39
Ok, then it's because of the java modules. I'll update my answer
– Svetlin Zarev
Jan 3 at 12:41
Thanks for explanation. Changing from Class.class.getResourceAsStream(file) to TextTools.class.getResourceAsStream(file) solve Error.
– Alexey
Jan 3 at 12:59
add a comment |
I've tried your application with both openjdk 8 and 11 on MacOS and it does not work with both. I think you need to look at [1] and [2] in order to understand how getResourceAsStream
works.
TLDR:
If the path is absolute (i.e. starts with a slash -
/
), thenclass.getResourceAsStream()
searches in the provided pathIf the path is NOT absolute (i.e. does not start with a slash) , then
class.getResourceAsStream()
searches in a constructed path that corresponds to the package name, where the dots are replaced with slashes
So whether it works or not depends on 2 things:
- Is your path absolute or not ?
- Is the
file
located in the same package as the class or not ?
Basically in your exaple as is provided, it can never work if the path is not absolute, because Class.class.getResourceAsStream()
will always resolve the path to java/lang/<file>
, so your file must be in a system package. So instead you must use <MyClass>.class.getResourceAsStream()
or alternatively use an absolute path
[1] https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)
[2] https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29
Update
Since Java SE 9, invoking getResourceXXX on a class in a named module will only locate the resource in that module, it will not search the class path as it did in previous release. So when you use Class.class.getResourceAsStream()
it will attempt to locate the resource in module containing java.lang.Class
, which is the java.base
module. Obviously your resource is not in that module, so it returns null.
You have to make java 9+ search for the file in your module, which most probably is an "unnamed module". You can do that by changing Class
to any class defined in your module in order to make java use the proper class loader.
I've tried your application with both openjdk 8 and 11 on MacOS and it does not work with both. I think you need to look at [1] and [2] in order to understand how getResourceAsStream
works.
TLDR:
If the path is absolute (i.e. starts with a slash -
/
), thenclass.getResourceAsStream()
searches in the provided pathIf the path is NOT absolute (i.e. does not start with a slash) , then
class.getResourceAsStream()
searches in a constructed path that corresponds to the package name, where the dots are replaced with slashes
So whether it works or not depends on 2 things:
- Is your path absolute or not ?
- Is the
file
located in the same package as the class or not ?
Basically in your exaple as is provided, it can never work if the path is not absolute, because Class.class.getResourceAsStream()
will always resolve the path to java/lang/<file>
, so your file must be in a system package. So instead you must use <MyClass>.class.getResourceAsStream()
or alternatively use an absolute path
[1] https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)
[2] https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource%28java.lang.String%29
Update
Since Java SE 9, invoking getResourceXXX on a class in a named module will only locate the resource in that module, it will not search the class path as it did in previous release. So when you use Class.class.getResourceAsStream()
it will attempt to locate the resource in module containing java.lang.Class
, which is the java.base
module. Obviously your resource is not in that module, so it returns null.
You have to make java 9+ search for the file in your module, which most probably is an "unnamed module". You can do that by changing Class
to any class defined in your module in order to make java use the proper class loader.
edited Jan 7 at 5:09
answered Jan 3 at 12:20
Svetlin ZarevSvetlin Zarev
6,00532760
6,00532760
I'm sorry if I am confused you. Yes, the path is absolute. TXT file in the package folder with Java class files and included in the Build Path. File Path: /configuration/options.txt
– Alexey
Jan 3 at 12:39
Ok, then it's because of the java modules. I'll update my answer
– Svetlin Zarev
Jan 3 at 12:41
Thanks for explanation. Changing from Class.class.getResourceAsStream(file) to TextTools.class.getResourceAsStream(file) solve Error.
– Alexey
Jan 3 at 12:59
add a comment |
I'm sorry if I am confused you. Yes, the path is absolute. TXT file in the package folder with Java class files and included in the Build Path. File Path: /configuration/options.txt
– Alexey
Jan 3 at 12:39
Ok, then it's because of the java modules. I'll update my answer
– Svetlin Zarev
Jan 3 at 12:41
Thanks for explanation. Changing from Class.class.getResourceAsStream(file) to TextTools.class.getResourceAsStream(file) solve Error.
– Alexey
Jan 3 at 12:59
I'm sorry if I am confused you. Yes, the path is absolute. TXT file in the package folder with Java class files and included in the Build Path. File Path: /configuration/options.txt
– Alexey
Jan 3 at 12:39
I'm sorry if I am confused you. Yes, the path is absolute. TXT file in the package folder with Java class files and included in the Build Path. File Path: /configuration/options.txt
– Alexey
Jan 3 at 12:39
Ok, then it's because of the java modules. I'll update my answer
– Svetlin Zarev
Jan 3 at 12:41
Ok, then it's because of the java modules. I'll update my answer
– Svetlin Zarev
Jan 3 at 12:41
Thanks for explanation. Changing from Class.class.getResourceAsStream(file) to TextTools.class.getResourceAsStream(file) solve Error.
– Alexey
Jan 3 at 12:59
Thanks for explanation. Changing from Class.class.getResourceAsStream(file) to TextTools.class.getResourceAsStream(file) solve Error.
– Alexey
Jan 3 at 12:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021754%2fwhy-does-the-getresource-method-return-null-in-jdk-11%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's a typical value for the 'file' argument you are using?
– DodgyCodeException
Jan 3 at 12:04
Please check this question out
– caco3
Jan 3 at 12:05
4
Class.class.getResourceAsStream(...) will attempt to locate the resource in the java.base module. Replace "Class.class" with the name of your class so that it finds it in your module (or on the class path).
– Alan Bateman
Jan 3 at 12:48
Further, it’s worth learning about the try-with-resources statement…
– Holger
Jan 10 at 15:29