getResourceAsStream returns null
Lifepaths.class.getClass().getResourceAsStream(...)
loads resources using system class loader, it obviously fails because it does not see your JARs
Lifepaths.class.getResourceAsStream(...)
loads resources using the same class loader that loaded Lifepaths class and it should have access to resources in your JARs
getResourceAsStream() is always returning null
To my knowledge the file has to be right in the folder where the 'this'
class resides, i.e. not in WEB-INF/classes
but nested even deeper (unless you write in a default package):
net/domain/pkg1/MyClass.java
net/domain/pkg1/abc.txt
Putting the file in to your java sources should work, compiler copies that file together with class files.
Why would ClassLoader.getResourceAsStream() return null?
I solved the mystery.
The key to solving was embedding some diagnostic logging when propertiesStream
is null:
String classpath = System.getProperty("java.class.path");
LOG.info("CLASSPATH: " + classpath);
ClassLoader loader = MyClientMain.class.getClassLoader();
System.out.println("ClassLoader resource path: " + loader.getResource("resource.properties"));
So when I run with the original
contextClassLoader.getResourceAsStream("resource.properties")
I receive the null pointer condition, printing:
INFO: CLASSPATH: myproj.one-jar.jar
ClassLoader resource path: null
.
I then started suspecting something related to the "jar within a jar" as this is what the com.simontuffs.onejar essentially does (i.e. wrapping my project's jar inside a jar that contains all other library jars), so I opened myproj.one-jar.jar with 7-Zip and noted the full (absolute) path of "resource.properties":
myproj.one-jar.jar\main\myproj.jar\webapp\WEB-INF\classes\resource.properties
.
So I modified getResource("resource.properties")
to:
getResource("/main/myproj.jar/webapp/WEB-INF/classes/resource.properties")
which didn't fix the problem but printed the following upon the null pointer condition:
INFO: CLASSPATH: myproj.one-jar.jar
ClassLoader resource path: jar:file:/myproj.one-jar.jar!/main/myproj.jar!//main/myproj.jar/webapp/WEB-INF/classes/resource.properties
.
Then... divine intervention fell upon me and I had the insight (not reading any documentation that could even hint this, I swear!) that I should be using this path instead:
getResource("/webapp/WEB-INF/classes/resource.properties")
And Voila! It works.
Whew.
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.
getResourceAsStream returning null despite called file being in same dir as class getResourceAsStream is called in
Created a dir called resources under /src/main/ and placed AwsCredentials.properties there and used
properties.load( PropertyLoader.class.getClassLoader().getResourceAsStream( "AwsCredentials.properties" ) );
instead of
properties.load( this.getClass().getResourceAsStream("AwsCredentials.properties" ) );
Not as elegant as I would like, but it works.
GetResourceAsStream returning null, file exists
You appear to be using the wrong ClassLoader
. Invoking context.class.getClassLoader()
provides the ClassLoader
with which the ServletContext
class (context.class
) was loaded. What you want is the ClassLoader
for the web application's classes, which would be context.getClassLoader()
.
getResourceAsStream returning null in Java 10
I found a solution although I don't entirely understand why this works an the problematic line doesn't, but this works:
Application.class.getResourceAsStream("/application.properties")
where Application
is just a class in my app.
Maybe this is related to the answer pointed to by ochi, and Application.class
is using my class loader and appProperties.getClass()
is using the system class loader. But why does it behave differently on differently on Java 8 vs 10 is not something that is apparent.
Related Topics
How to Limit the Number of Characters in Jtextfield
When Should We Use Observer and Observable
How Apply CSS on a <H:Inputtext>
What Is Difference Between @+Id/Android:List and @+Id/List
How to Sort Alphabetically While Ignoring Case Sensitive
Android: How to Handle Button Click
Get Application Context from Non Activity Singleton Class
Unsupported Major.Minor Version 52.0 in My App
How to Get Eclipse to Use a Different Compiler Version for Java
Configuring Log4J Loggers Programmatically
Unobtrusive Way to Combine and Compress JavaScript/CSS for Java/Spring/Maven Applications
Preemptive Basic Auth with Httpurlconnection
How to Save Bitmap to Android Gallery
Android Simpledateformat, How to Use It