Questo sito è ancora in costruzione.
L'attuale sito ufficiale del JUG Padova è all'indirizzo www.jugpadova.it

Accessing a resource within a .jar

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()    .getResourceAsStream("yourpackage/mypackage/myfile.xml");

It will run inside or outside the jar. Enjoy !