I\'m trying to access a file in my project. But getResource method returns null.
This is how my project looks like:
Try using /
prefixing.
Thread.currentThread().getContextClassLoader().getResourceAsStream("/xxx.png")
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.
Use the following code, it should work.
YOUR_CLASS_HERE.class.getClass().getResource( "/xxx.png" );
e.g.
Signin.class.getClass().getResource( "/xxx.png" );
Either Approach will work. its just Filepath issue.
Try to remove "assets" from below line of code.
Thread.currentThread().getContextClassLoader().getResource("assets/xxx.png"); //returns null
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").