Reputation: 501
Had no luck getting this to work via what I found on google.
Upvotes: 2
Views: 164
Reputation: 12206
You can load any resource that is available on the classpath, which your jar would be a part of, using ClassLoader.getResource(String)
You can obtain a reference to a relevant ClassLoader
object using Class.getClassLoader()
ClassLoader cl = MyClass.class.getClassLoader();
If your image were named myimage.png
and in a directory called images
inside your jar, you could get the image like so.
URL url = cl.getResource("images/myimage.png");
You can then use the URL to create an image object in memory.
Image i = Toolkit.getDefaultToolkit().createImage(url);
Upvotes: 3
Reputation: 1280
The tutorial on How to Use Icons at the Java website may be of use to you.
You'll do something like this:
java.net.URL imgURL = this.getClass().getResource(path);
The getResource()
method uses the same loading rules as defined by the ClassLoader
for the class.
Upvotes: 3