Accessing a resource within a .jar
Posted by Emanuele Gesuato Sun, 05 Feb 2006 18:08:00 GMT
Sometimes it is useful to distribute an application in a jar file through Java Web Start or any other way. So, you could have to read some resource (images or properties file) from inside a jar. How can you do it ? It’s very simple, here’s an example to retrieve an image:
ImageIcon image = (new ImageIcon(getClass().getResource("yourpackage/mypackage/image.gif")));
In general, you can retrieve an InputStream in the following way:
InputStream is = this.getClass().getClassLoader()
It will run inside or outside the jar. Enjoy !