tags:

views:

15

answers:

1

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?

+1  A: 

What do you need the path for? To construct a FileInputStream with? If all what you want to do is to get an InputStream of the image, then just use Class#getResourceAsStream() instead.

BalusC
I'm using the Java GTK bindings, and I thought that I couldn't create a GTK Image object without using the path directly. But upon reading the documentation more closely and reading your suggestion, it turns out I can create one from a byte array, so thanks!
jvedi
In other words, you're going to hardcode the image as a byte array?
BalusC