I want to get the path to a resource for ImageIO to read out a BufferedImage from some .png s.
While developing the project I use a relative path to “/bin/stuff/icons/image.png” , but this will definetly not work when I put everything together into a .jar file, so I need a way to get the path to these resources both while testing in eclipse and when later running it within a .jar .
I want to get the path to a resource for ImageIO to read out
Share
After a lot of trying out both finding the file and getting the input stream to the file I came to the conclusion that this approach works every time:
Where path is
as found in the src directory of the eclipse project.
E.g.
When getting only the resource and then getting the path of the resource to link to a file, you will get FileNotFoundExceptions when using a .jar (but not while testing with eclipse, so one should be warned to think that his code works).
And – no – I don’t save images in the bin/ – but they are copied to this directory and thus I find them there while testing. Now everything seems to be working.