Why does getResource return null
Asked Answered
S

6

6

I'm trying to access a file in my project. But getResource method returns null.

This is how my project looks like: enter image description here

Thread.currentThread().getContextClassLoader().getResource("assets/xxx.png"); //returns null

And how project folder in eclipse workspace looks like:

enter image description here

Why? I want to access files in my assets folder?

Edit I created a jar file and this is content of the jar:

enter image description here

Solved

First of all, I've a lot of image files so I want to organize all them in a folder. I put the assets folder in src directory and finally I was able to access the files.

this.getClass().getClassLoader().getResource("assets/xxx.png");

enter image description here

Schoolmistress answered 26/4, 2015 at 12:46 Comment(5)
You can't access the assert folder unless otherwise you have bundled it in your jar. Can you extract a jar and show the content of so that we can help you.Anna
Is asset folder defined as source folder?Fountainhead
@Fountainhead yes, defined as source folderSchoolmistress
@Anna I did it.. I edited my post, can you take a look?Schoolmistress
assets/ should not be in the path. It's an outside (not in the src) folder in the build path, which just means its contents are put into the root, without the directory. See hereDurwood
A
2

There are lot of ways to add a resource to the jar file, you can put it in src, add as a resource if you use maven, ant etc... If you able to bundle whole directory then you should be able to use your original piece of code. With the current structure you can use following piece of code.

Thread.currentThread().getContextClassLoader().getResource("/xxx.png"). 
Anna answered 26/4, 2015 at 13:10 Comment(3)
If I simply add all files to bin directory I can access. But there are many files in my project and I want to organize them in a directory. So I want to put all files in a folder and access them from this directory.Schoolmistress
Put the whole asserts directory in src (source) folder. Eclipse will bundle that for you.Anna
@Anna - Your code seems to get a relative path. What is it relative to and how can I find out the root ?Heterocercal
P
2

For someone struggling as me. For Maven just run mvn clean install. After that Thread.currentThread().getContextClassLoader().getResource() should work.

Priscella answered 30/6, 2021 at 9:31 Comment(0)
S
1

Try using / prefixing.

Thread.currentThread().getContextClassLoader().getResourceAsStream("/xxx.png")

Supererogate answered 26/4, 2015 at 12:58 Comment(2)
as per your jar structure i can see that image file in root. i updated my answer try once again. ThanksSupererogate
try using getResourceAsStream, and change your class to take Inputstream instead of File.Supererogate
T
0

Is there a reason you're using the the class loader of the current class? Something like this.getClass().getClassLoader().getResource("/xxx.png") should be more reliable.

Thorin answered 26/4, 2015 at 13:10 Comment(0)
S
0

Use the following code, it should work.

YOUR_CLASS_HERE.class.getClass().getResource( "/xxx.png" );

e.g. Signin.class.getClass().getResource( "/xxx.png" );

Shamus answered 26/4, 2015 at 13:13 Comment(0)
L
0

Either Approach will work. its just Filepath issue.

  • Your Jar Structure shows no "asset" Folder
    xxx.png file is directly in Jar File.

Try to remove "assets" from below line of code.

Thread.currentThread().getContextClassLoader().getResource("assets/xxx.png"); //returns null
  • Also, if you want to use "assets" folder in ur classpath, please ensure that your jar contains "assets" folder.
Lovesick answered 26/4, 2015 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.