Accessing Resources in a JAR File
15 April 2008JAR files are used to deploy applications. They comprise of java classes and other resources like images etc. In this post, I will explain how to access resources packed in a JAR file.
To access resources in a JAR file, we use getResource method. Lets do this practically. The code snippet provided shows how to retrieve images from a JAR file.
// Get current classloader ClassLoader cl = this.getClass().getClassLoader(); // Create icons Icon saveIcon = new ImageIcon(cl.getResource("images/save.gif")); Icon cutIcon = new ImageIcon(cl.getResource("images/cut.gif"));
The example assumes that the following entries exist in the JAR file.
-images/save.gif
-images/cut.gif
Hope this helps.
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Accessing Resources in a JAR File