I’m writing an app in Java in Eclipse where I need to get the absolute path to an image I’m using. I decided to use getClass().getResource().getPath(), and it works great when I’m running the app from Eclipse.
When I export the app to a JAR file, however, the image doesn’t appear. I put in a print statement to find out what was coming from the call above, and it turns out that in Eclipse it comes back with something like “/some/path/to/image.jpg” and when I run it from the JAR, it comes back with “file:/some/path/to/image.jpg”. I know the extra “file:” is what is causing the problem, but if I’m using getPath(), it shouldn’t be there. Even weirder is why does it show up from the JAR but not in Eclipse?
What do you need the path for? To construct a
FileInputStreamwith? If all what you want to do is to get anInputStreamof the image, then just useClass#getResourceAsStream()instead.