user1012037
user1012037

Reputation: 501

Java applet from jar

Had no luck getting this to work via what I found on google.

Upvotes: 2

Views: 164

Answers (2)

Dev
Dev

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

Milind Ganjoo
Milind Ganjoo

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

Related Questions