Accessing Resources in a JAR File

15 April 2008

JAR 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.

del.icio.us:Accessing Resources in a JAR File  digg:Accessing Resources in a JAR File  spurl:Accessing Resources in a JAR File  wists:Accessing Resources in a JAR File  simpy:Accessing Resources in a JAR File  newsvine:Accessing Resources in a JAR File  blinklist:Accessing Resources in a JAR File  furl:Accessing Resources in a JAR File  reddit:Accessing Resources in a JAR File  fark:Accessing Resources in a JAR File  blogmarks:Accessing Resources in a JAR File  Y!:Accessing Resources in a JAR File  smarking:Accessing Resources in a JAR File  magnolia:Accessing Resources in a JAR File  segnalo:Accessing Resources in a JAR File  gifttagging:Accessing Resources in a JAR File

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

Leave a Reply