Why does the getResource() method return null in JDK 11?
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.
getClass().getResource() always returning null
I had to clean the build and then rebuild. Works now.
getClass() getResource returning null
Replace this.getClass().getResource("res/bg.jpg")
with this.getClass().getResource("../res/bg.jpg")
Since this.getClass().getResource("/")
returns file:/C:/Users/cmooney/eclipse-workspace/TextSimplifier/bin/
, you need to go one level(directory) up in the directory structure so that you can enter the res
directory. It's like cd ../res
from the current location of file:/C:/Users/cmooney/eclipse-workspace/TextSimplifier/bin/
Note: I can't see bg.jpg
in the screenshot that you have attached. Make sure, you have bg.jpg
in this path.
Java class.getResource() returns null
Files in the resources
folder will be packaged to the root of the .jar file, meaning that during development, the resources
folder itself is in the classpath, so you need this.getClass().getResource("/table.1gram")
, or without the /
since your class is in the unnamed package, aka also in the root of the .jar file.
Why does class.getResource() keep returning null although there is a resource at the specified path?
Short answer: Use "/img/smile.png"
.
What's actually happening is that any path starting with /
which is given to the Class.getResource method is always treated as being relative to each entry in the classpath.
As your screenshot shows, the res
directory is such a classpath entry. So the Class.getResource method treats the path you provide as relative to that entry. Meaning, relative to the res
directory.
So, the method combines your string argument with that directory, which results in res/res/img/smile.png
. Since no file (resource) exists at that location, it returns null.
Related Topics
Servlet Seems to Handle Multiple Concurrent Browser Requests Synchronously
How to Disable or Bypass Hardware Graphics Acceleration(Prism) in Javafx
Dependency Injection and Javafx
How to Read Input Character-By-Character in Java
How to Make Jscrollpane (In Borderlayout, Containing JPAnel) Smoothly Autoscroll
Java's Date(...) Constructor Is Deprecated; What Does That Mean
Singleton Design Pattern: Pitfalls
How to Keep the Iteration Order of a List When Using Collections.Tomap() on a Stream
Simple Way to Count Character Occurrences in a String
How to List All Classes Loaded in a Specific Class Loader
Java Thread Priority Has No Effect
Passing Directly an Array Initializer to a Method Parameter Doesn't Work
Programmatically Change Log Level in Log4J2
Differencebetween 'Super' and 'Extends' in Java Generics
Java: How to Indent Xml Generated by Transformer
How to Set Up Java Logging Using a Properties File? (Java.Util.Logging)