Meaningless Drivel is fun!
The moose likes Java in General and the fly likes Resources in JAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Resources in JAR" Watch "Resources in JAR" New topic

Resources in JAR

Ikram Soomro
Ranch Hand

Joined: Oct 20, 2001
Posts: 42
can we place images and other IO Files (Resources other than .class files) in jar that are used in application ? if yes than how?
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

In general, Yes.
Just as you would place class files in a jar; by using the jar tool. jar command doesnt differentiate between class files or any other files, all files in the target directory are included in the generated jar.


The future is here. It's just not evenly distributed yet. - William Gibson
Sonny Gill LinkedIn
Jim Hicks

Joined: Jun 23, 2004
Posts: 11
A classloader loads the jars so, the files in a jar are accessed from the classloader. Use the method classloader.getResourceAsStream(String), or URL classloader.getResource(String).

To make sure you are using the same classloader as the one that loaded the jar, you should get the classloader from a object who's class is in the jar.

Classloader classloader = someObject.getClass().getClassloader();

If you want to access the jar with jar apis, you can get a url then use jar api's.
I agree. Here's the link:
subject: Resources in JAR
jQuery in Action, 3rd edition