Stuff Stuff in JARs #85
Chapter 11, Native Integration and Packaging
An Obvious Secret
JAR files—the acronym is short for Java ARchive—must be the best-known,
least-used feature in Java. Many developers throw a JAR in their classpath to
pick up some standard extension API or third-party library, but how many
actually distribute their software that way?
And JARs aren’t just about code. It’s really easy to put the files your pro-
gram needs into a JAR. This has the added advantage of hiding your images,
sounds, default settings, and so forth from end users.
But to load these items, you need to make a change in how you load stuff in
your code. Instead of specifying a known path or URL, you ask a
ClassLoader to find these resources along the classpath. By doing this, you
can get your resources from flat files while you’re developing, and then eas-
ily switch to getting them from inside a JAR when the code is deployed in
The key is the
ClassLoader’s getResource( ) and getResources( ) methods,
which take a path relative to the loaded class and return a
URL and an array
URLs respectively. A getResourceAsStream( ) method converts the URL to an
InputStream as a convenience.
To clarify the relative path: say you have a directory that includes your com-
piled classes in a path like com/mycompany/mypackage/…, an images direc-
tory, and a sounds directory. A relative path would be one of the form
images/something.png, sounds/something.aiff, etc. By using a resource on the
classpath, there is no difference (to the user) between files in a JAR and files
in sub-directories on a filesystem. Either way, you get a
URL that you can use
by passing it to methods that take
URL arguments, by opening an
InputStream from it, etc.
Example 11-7 shows an example of this technique. It uses the getResource( )
method to load an image and put it in an ImageIcon, which in turn is used to
JButton. It also loads in a sound, which is played via JavaSound
when you click the button.
Example 11-7. Loading image and sound as resources along the classpath