'java getResource() not working
this is driving me crazy. I have a NetBeans project in a folder with the following structure:
MyProject
---- build
---- src
---- resources
in src my code is in packages. What I am trying to do is to use
getClass().getResource("/resources/new.png");
from a class in package com.my.package but it just refuses to work! The "new.png" image is in the resources folder. Am I missing something here?
After a lot of playing around and moving the "new.png" image here and there to see when it will find the image, it finally worked but only when I put the image in the build folder. So what do I have to do to make this work ?
edit: sorry, wrong String in method parameter. Corrected now
Solution 1:[1]
This works fine:
BufferedImage image = ImageIO.read(getClass().getClassLoader().getResourceAsStream("file_name"));
Solution 2:[2]
look out with ImageIO.read(getClass().getClassLoader().getResourceAsStream("file_name"));
...on windows you will get a URL encoded string, with %20 for spaces, so if you want to use that path to load a file, first call pathString.replace("%20", " ") ...that $hice just nailed me in live!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | S. Esteves |
| Solution 2 | tom |
