Tuesday, February 23, 2010

Creating a JAR File

Sun Java
  • Java Developer Tools

    See and download all software tools available from Sun.

  • Java Standard Edition

    For developing and deploying Java applications for the desktop, servers, embedded, and real-time environments.

  • Java Enterprise Edition

    For enterprise, server-side Java applications.

  • Java Micro Edition

    For Java applications running on mobile devices.

  • Java Training

    Sharpen your Java skills with courses from the source.

  • Java Support

    Get dedicated help from Sun including technical assistance, product support, and support for deployed Java applications.


Solaris
  • OpenSolaris

    Download, develop and collaborate with OpenSolaris

  • Solaris

    Download the most advanced operating system in the world

  • Sun Studio

    Optimizing compilers and tools for C/C++/Fortran application development

  • Solaris Developer Center

    Explore the resources and community available to the Solaris developer.

  • Sun Developer Services

    Get technical assistance, product support, training, and other services from the source.

  • BigAdmin

    A community site with Solaris system administration information, hardware compatibility, a script library, and other resources for administrators of Sun products.

Communities
  • OpenJDK

    The place to collaborate on the open-source JDK, an implementation of the Java Platform, Standard Edition specification.

  • Mobile & Embedded

    The Mobile & Embedded Community enables and empowers developers to collaborate and innovate, driving the evolution and adoption of the Java(TM) Platform, Micro Edition (Java ME) for mobile and embedded devices.

  • GlassFish

    The GlassFish community is building free, open source, production-quality, enterprise software.

  • NetBeans

    You have the opportunity to submit bugs and feature requests in IssueZilla, submit news for the NetBeans Community, and contribute code or even create a project of your own. Welcome to the team!

  • OpenSolaris

    The OpenSolaris source code is already cutting edge, but innovation happens everywhere, so we welcome your involvement.

  • OpenSPARC

    OpenSPARC.net is the genesis of a vision to create a larger community where open conversations and collaborative development projects spawn dramatic innovations around chip design.

  • Open Storage

    The OpenSolaris storage community is your gateway to data management related communities and projects - file sharing, file systems, volume managers, data services, storage drivers, and much more.

  • OpenJFX

    Project OpenJFX is a community for sharing early versions of the JavaFX Script language and for collaborating on its development.

  • java.net

    A gathering place for Java technology enthusiasts and existing communities across industries, platforms, and interest groups.

  • Sun Student Developers

    The SDN Academic Developer Program offers you ready access to tools, resources, and student communities.

  • Java Community Process

    The JCP gives you a chance to both have your own work become an official component of the Java platform, and to offer suggestions for improving and growing the technology.


My SDN Account


Using JAR Files: The Basics

Creating a JAR File


Book Excerpt Index

The basic format of the command for creating a JAR file is:

jar cf jar-file input-file(s)

Let's look at the options and arguments used in this command:

  • The c option indicates that you want to create a JAR file.
  • The f option indicates that you want the output to go to a file rather than to stdout.
  • jar-file is the name that you want the resulting JAR file to have. You can use any filename for a JAR file. By convention, JAR filenames are given a .jar extension, though this is not required.
  • The input-file(s) argument is a space-delimited list of one or more files that you want to be placed in your JAR file. The input-file(s) argument can contain the wildcard * symbol. If any of the "input-files" are directories, the contents of those directories are added to the JAR archive recursively.

The c and f options can appear in either order, but there must not be any space between them.

This command will generate a compressed JAR file and place it in the current directory. The command will also generate a default manifest file for the JAR archive.

You can add any of these additional options to the cf options of the basic command:

Option Description
v Produces verbose output on stderr (in version 1.1) or stdout (in version 1.2) while the JAR file is being built. The verbose output tells you the name of each file as it's added to the JAR file.
0 (zero) Indicates that you don't want the JAR file to be compressed.
M Indicates that the default manifest file should not be produced.
m Used to include manifest information from an existing manifest file. The format for using this option is:
jar cmf existing-manifest jar-file input-file(s)
See Modifying a Manifest for more information about his option.
-C To change directories during execution of the command. Version 1.2 only. See below for an example.

In version 1.1, the JAR-file format supports only ASCII filenames. Version 1.2 adds support for UTF8-encoded names.

An Example

Let's look at an example. The JDK demos include a simple TicTacToe applet. This demo contains a bytecode class file, audio files, and images all housed in a directory called TicTacToe having this structure:

The audio and images subdirectories contain sound files and GIF images used by the applet.

To package this demo into a single JAR file named TicTacToe.jar, you would run this command from inside the TicTacToe directory:

jar cvf TicTacToe.jar TicTacToe.class audio images

The audio and images arguments represent directories, so the Jar tool will recursively place them and their contents in the JAR file. The generated JAR file TicTacToe.jar will be placed in the current directory. Because the command used the v option for verbose output, you'd see something similar to this output when you run the command:

adding: TicTacToe.class
(in=3825) (out=2222) (deflated 41%)
adding: audio/ (in=0) (out=0) (stored 0%)
adding: audio/beep.au
(in=4032) (out=3572) (deflated 11%)
adding: audio/ding.au
(in=2566) (out=2055) (deflated 19%)
adding: audio/return.au
(in=6558) (out=4401) (deflated 32%)
adding: audio/yahoo1.au
(in=7834) (out=6985) (deflated 10%)
adding: audio/yahoo2.au
(in=7463) (out=4607) (deflated 38%)
adding: images/ (in=0) (out=0) (stored 0%)
adding: images/cross.gif
(in=157) (out=160) (deflated -1%)
adding: images/not.gif
(in=158) (out=161) (deflated -1%)

You can see from this output that the JAR file TicTacToe.jar is compressed. The Jar tool compresses files by default. You can turn off the compression feature by using the 0 (zero) option, so that the command would look like:

jar cvf0 TicTacToe.jar TicTacToe.class audio images

You might want to avoid compression, for example, to increase the speed with which a JAR file could be loaded by a browser. Uncompressed JAR files can generally be loaded more quickly than compressed files because the need to decompress the files during loading is eliminated. However, there's a tradeoff in that download time over a network may be longer for larger, uncompressed files.

The Jar tool will accept arguments that use the wildcard * symbol. As long as there weren't any unwanted files in the TicTacToe directory, you could have used this alternative command to construct the JAR file:

jar cvf TicTacToe.jar *

Though the verbose output doesn't indicate it, the Jar tool automatically adds a manifest file to the JAR archive with pathname META-INF/MANIFEST.MF. See the Understanding the Manifest section for information about manifest files.

In the above example, the files in the archive retained their relative pathnames and directory structure. The Jar tool in version 1.2 of the Java Development Kit provides the -C option that you can use to create a JAR file in which the relative paths of the archived files are not preserved. It's modeled after GZIP's -C option.

As an example, suppose you wanted put audio files and gif images used by the TicTacToe demo into a JAR file, and that you wanted all the files to be on the top level, with no directory hierarchy. You could accomplish that by issuing this command from the parent directory of the images and audio directories:

jar cf ImageAudio.jar -C images * -C audio *

The -C images part of this command directs the Jar tool to go to the images directory, and the * following -C images directs the Jar tool to archive all the contents of that directory. The -C audio * part of the command then does the same with the audio directory. The resulting JAR file would have this table of contents:

META-INF/MANIFEST.MF
cross.gif
not.gif
beep.au
ding.au
return.au
yahoo1.au
yahoo2.au

By contrast, suppose that you used a command that didn't employ the -C option:

jar cf ImageAudio.jar images audio

The resulting JAR file would have this table of contents:

META-INF/MANIFEST.MF
images/cross.gif
images/not.gif
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.



Feedback

No comments:

Post a Comment