
432
|
Chapter 11, Native Integration and Packaging
#85 Stuff Stuff in JARs
HACK
Packing Up
So far, this only proves that resource loading is a nice alternative to fully
specified paths. The next step is to put the application and its resources in a
JAR. You can do this with a single command:
jar cf buh.jar JarResourceLoading.class images sounds
Now, you run the application from the JAR by pointing the classpath into it.
Here’s what the output from that looks like:
[tonberry:] cadamson% java -classpath buh.jar JarResourceLoading
found image at jar:file:/Users/cadamson/Documents/O'Reilly/books/
swing%20hacks/HacksBook/PackagingInstalling/97/buh.jar!/images/
keagan-buh.jpeg
found sound at jar:file:/Users/cadamson/Documents/O'Reilly/books/
swing%20hacks/HacksBook/PackagingInstalling/97/buh.jar!/sounds/
buhbuhbuh.aiff
Note the different URLs: the image and sound are now found inside buh.jar.
The format of the
jar: URL is also interesting, in that it combines a regular
file:-type URL for the JAR file with a path inside that JAR, using a ! char-
acter to separate the two parts.
Double-Clicking JARs
There are a few more useful things you can do with JAR files. The first is
that you can eliminate the need to specify a main class on the command line
by specifying it in the JAR file instead. This has the advantage of making the
JAR a double-clickable application on graphical operating systems. To do
this, create ...