
Stuff Stuff in JARs #85
Chapter 11, Native Integration and Packaging
|
429
HACK
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 field.
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
of
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 ...