| Home > Core Java FAQ
> Miscellaneous FAQ |
| Miscellaneous |
| java.lang
& java.util Package(08) * Sound(14)
* Jar (06) |
| |
|
Q . Is there any Tuturial available on jars?
|
Ans :
There is a Jar Files
tutorial available from SUN..!
|
|
Q .
How can I put all my classes and resources into one file and have java run it?
|
Ans
:
Use a JAR file. Put all the files in a JAR, then run the app like
this:
java -jar [-options] jarfile Mainclass [args...]
|
|
Q . Which program is used to create .zip files compatible with the java* programs?
|
Ans
:
Use the jar-tool from JDK1.1(.1):
jar [ options ] [manifest] destination input-file [input-files]
E.g.:
jar cvf myJarFile.jar *.class
creates a compressed archive. And watch out -- the order of the options
("cvf") determine the order of the arguments!
jar cvfO myJarFile.zip *.class
creates it fullsize (uncompressed) (note the 'O'-option used for JDK1.0.2)
On Unix you can also use:
zip -rn ".class" my_file.zip *
Info-ZIP home page: http://www.cdrom.com/pub/infozip/
Latest source code: ftp://ftp.uu.net/pub/archiving/zip/src/zip21.zip
Netscape's command line version of its JAR packager and signing tool is called "zigbert". They also have a signing tool with GUI written in
Java. More info
http://developer.netscape.com/software/signedobj/jarpack.html
If you zip your .class files for JDK 1.0.2 (for 1.1 you'll use a Jar):
1. zip your files uncompressed (can use WinZip 6.2 up); Unix command:
zip -r0 classes.zip <directories>
2. Make sure the main class has no parent directory inside the archive, (in other words, don't build an archive with
foo/bar/myMain.class, unless your myMain is in a package called
foo.bar. Instead start it at myMain.class). Your packages must be placed in the archive using their corresponding filesystem
pathnames.
3. Put the archive in the same directory as the .html page.
4. Put something like the following tag in the .html file:
<APPLET CODEBASE="." ARCHIVE=my_zip_file.zip,myOtherZip.zip,thirdfile.zip
CODE="my_main_class.class" WIDTH=600 HEIGHT=250>
</APPLET>
From JDK 1.1 on, an example of the applet tag used with a jar file is
<APPLET ARCHIVE=myfile.jar CODE=myapplet.class WIDTH=600 HEIGHT=250>
</APPLET>
These lines will use an applet called myapplet that can be found in the jarfile myfile.jar. An example applet tag of a jar file used to hold
classes in packages is
<APPLET ARCHIVE="myclasses.jar" CODE="linden.net.MyApplet.class"
WIDTH=480
HEIGHT=120>
</APPLET>
You can supply several jar filenames in a comma-separated list. Jar files are in compressed PKZIP format.
|
|
Q . What is the difference between the various ZIP formats: ZIP, GZIP, and PKZIP?
|
Ans
:
Zip is an archive file format, popularized on PCs, that contains
multiple compressed files.
GZIP comes from Gnu. It is essentially a one file subset of the Zip format. You can't put a whole directory into a GZIP archive, just a
single file. It's intended for compressing a tarball of many files.
PKZIP is a set of commercially available programs that create Zip files. All three use the deflate compression format, which is based on
the LZ77 algorithm. This compression is also used by the ZLIB library
and hence the PNG graphics file format (which uses ZLIB). PNG - Portable Network Graphics - provides a patent-free replacement for GIF
and TIFF.
An alternative compression technology, LZW compression, is encumbered by Unisys's patent. LZW is used in GIF files and by the Unix compress
command. Luckily, as well as being free from patent restrictions, LZ77
also gives better compression than LZW. LZW is the initial letters of
the last names of the three computer scientists who developed the algorithm (Lempel, Ziv, Welch).
The basic classes (all in java.util.zip) that read LZ77 Zip format are Deflater and Inflater. These are used by the stream classes
DeflaterOutputStream and InflaterInputStream. The java.util.zip classes
GZIPInputStream and ZipInputStream inherit from InflaterInputStream.
PKZIP is a commercial program for DOS, Windows, and OS/2, sold by PKWARE Their FAQ, at
http://www.pkware.com/zipgfaq.html, specifically says
"Because PKWARE has dedicated the .ZIP file format to the public domain, it is possible for other people to write programs which
can read .ZIP files. NOTE THAT THE PKZIP, PKUNZIP, PKSFX PROGRAMS AND THEIR ASSOCIATED SOURCE CODE AND SUPPORT PROGRAMS ARE THE
EXCLUSIVE PROPERTY OF PKWARE INC. AND ARE NOT PUBLIC DOMAIN SOFTWARE PROGRAMS.
The "other people" PKZIP's FAQ refers to is the InfoZIP project, a group of public-minded programmers spread over the world producing free
software that works on most ANSI C compilers and platforms. See
http://www.cdrom.com/pub/infozip/.
Jar files are in ZIP format, but are not as complete as a full filesystem archive format since file permissions are not saved. Some
versions of WinZip are known to be inadequate for processing the full
PKZIP format. Use InfoZIP instead
|
|
Q . Which version of WinZip is compatible with java.util.zip?
|
Ans
:
You need WinZip version 6.2 or later. Version 6.1 or earlier is not
good enough. WinZip can be downloaded from http://www.winzip.com/download.cgi. The pkzip software works fine.
Infozip is better than WinZip because it lacks the winzip "feature" of
failing to recreate directories unless given a special option. Use
Infozip.
|
|
Q . My executable Jar files don't work!
|
Ans :
It is possible to exactly follow the instructions given in the
documentation and yet still get the following error message when attempting to run the .jar file: "Failed to load Main-Class manifest
attribute from myapp.jar"
There has to be a carriage return after the Main-Class definition in the manifest file, otherwise it does not work! Example, in a file
called manifest.txt:
Main-Class: MyClass // does not work
Main-Class: MyClass
// does work
Then, when the manifest file is merged into the jar file like this:
jar cmfv manifest.txt myapp.jar *.class
it works fine.
|