{"id":365,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=365"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Creating a JAR File","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=365","title":{"rendered":"Creating a JAR File"},"content":{"rendered":"<blockquote><p>The basic format of the command for creating a JAR file is:<br \/>\n<BLOCKQUOTE><PRE>jar cf <I>jar-file input-file(s)<\/I><br \/>\n<\/PRE><\/BLOCKQUOTE>The options and arguments used in this command are:<br \/>\n<UL><br \/>\n<LI>The <TT>c<\/TT> option indicates that you want to <EM>create<\/EM> a JAR file. <\/LI><br \/>\n<LI>The <TT>f<\/TT> option indicates that you want the output to go to a <EM>file<\/EM> rather than to stdout. <\/LI><br \/>\n<LI><TT>jar-file<\/TT> 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 <TT>.jar<\/TT> extension, though this is not required. <\/LI><br \/>\n<LI>The <TT>input-file(s)<\/TT> argument is a space-separated list of one or more files that you want to include in your JAR file. The <TT>input-file(s)<\/TT> argument can contain the wildcard <TT>*<\/TT> symbol. If any of the &#8220;input-files&#8221; are directories, the contents of those directories are added to the JAR archive recursively. <\/LI><\/UL><br \/>\n<P>The <TT>c<\/TT> and <TT>f<\/TT> options can appear in either order, but there must not be any space between them. <\/P><br \/>\n<P>This command will generate a compressed JAR file and place it in the current directory. The command will also generate a <A class=TutorialLink href=\"http:\/\/www.strongd.net\/blog\/show\/359\" target=_top>default manifest file<\/A> for the JAR archive. <\/P><br \/>\n<P><\/P><br \/>\n<BLOCKQUOTE><br \/>\n<HR><br \/>\n<STRONG><FONT color=red>Note<\/FONT>:<\/STRONG>&nbsp;The metadata in the JAR file, such as the entry names, comments, and contents of the manifest, must be encoded in UTF8.<br \/>\n<HR><br \/>\n<\/BLOCKQUOTE><br \/>\n<P>You can add any of these additional options to the <TT>cf<\/TT> options of the basic command:<br \/>\n<TABLE cellPadding=3 border=1><br \/>\n<CAPTION><STRONG>jar command options <\/STRONG><\/CAPTION><br \/>\n<CAPTION><\/CAPTION><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TH>Option<\/TH><br \/>\n<TH>Description<\/TH><\/TR><br \/>\n<TR><br \/>\n<TD align=middle><TT>v<\/TT><\/TD><br \/>\n<TD>Produces <EM>verbose<\/EM> output on stdout while the JAR file is being built. The verbose output tells you the name of each file as it&#8217;s added to the JAR file.<\/TD><\/TR><br \/>\n<TR><br \/>\n<TD align=middle><TT>0<\/TT> (zero)<\/TD><br \/>\n<TD>Indicates that you don&#8217;t want the JAR file to be compressed.<\/TD><\/TR><br \/>\n<TR><br \/>\n<TD align=middle><TT>M<\/TT><\/TD><br \/>\n<TD>Indicates that the default manifest file should not be produced.<\/TD><\/TR><br \/>\n<TR><br \/>\n<TD align=middle><TT>m<\/TT><\/TD><br \/>\n<TD>Used to include manifest information from an existing manifest file. The format for using this option is:<br \/>\n<BLOCKQUOTE><PRE>jar cmf existing-manifest jar-file input-file(s)<br \/>\n<\/PRE><\/BLOCKQUOTE>See <A class=TutorialLink href=\"http:\/\/www.strongd.net\/blog\/show\/361\" target=_top>Modifying a Manifest File<\/A> for more information about his option.<br \/>\n<BLOCKQUOTE><br \/>\n<HR><br \/>\n<STRONG><FONT color=red>Warning<\/FONT>:<\/STRONG>&nbsp;The manifest must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.<br \/>\n<HR><br \/>\n<\/BLOCKQUOTE><\/TD><\/TR><br \/>\n<TR><br \/>\n<TD align=middle><TT>-C<\/TT><\/TD><br \/>\n<TD>To change directories during execution of the command. See below for an example.<\/TD><\/TR><\/TBODY><\/TABLE><\/P><br \/>\n<P><\/P><br \/>\n<BLOCKQUOTE><br \/>\n<HR><br \/>\n<STRONG><FONT color=red>Note<\/FONT>:<\/STRONG>&nbsp; When you create a JAR file, the time of creation is stored in the JAR file. Therefore, even if the contents of the JAR file do not change, when you create a JAR file multiple times, the resulting files are not exactly identical. You should be aware of this when you are using JAR files in a build environment. It is recommended that you use versioning information in the manifest file, rather than creation time, to control versions of a JAR file. See the <A class=TutorialLink href=\"http:\/\/www.strongd.net\/blog\/show\/354\" target=_top>Setting Package Version Information<\/A> section.<br \/>\n<HR><br \/>\n<\/BLOCKQUOTE><\/BLOCKQUOTE><A name=example><\/A><br \/>\n<H3><A name=example>An Example<\/A><\/H3><br \/>\n<BLOCKQUOTE><A name=example>Let us look at an example. A simple <TT>TicTacToe<\/TT> applet. You can see the source code of this Applet at <\/A><A href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/deployment\/jar\/examples\/TicTacToe.java\">TicTacToe.java<\/A>. This demo contains a bytecode class file, audio files, and images having this structure:<br \/>\n<CENTER><br \/>\n<P><IMG height=121 alt=\"TicTacToe folder Hierarchy\" src=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/figures\/deployment\/jar\/jar1.png\" width=338 align=bottom><\/P><br \/>\n<P class=FigureCaption><\/P><br \/>\n<CENTER><STRONG>TicTacToe folder Hierarchy<\/STRONG><\/CENTER><br \/>\n<P><\/P><\/CENTER><br \/>\n<P>The <TT>audio<\/TT> and <TT>images<\/TT> subdirectories contain sound files and GIF images used by the applet. <\/P><br \/>\n<P><\/P><br \/>\n<P>You can obtain all these files from <I>jar\/examples<\/I> directory when you download the entire Tutorial online. To package this demo into a single JAR file named <TT>TicTacToe.jar<\/TT>, you would run this command from inside the <TT>TicTacToe<\/TT> directory: <\/P><br \/>\n<BLOCKQUOTE><PRE>jar cvf TicTacToe.jar TicTacToe.class audio images<br \/>\n<\/PRE><\/BLOCKQUOTE>The <TT>audio<\/TT> and <TT>images<\/TT> arguments represent directories, so the Jar tool will recursively place them and their contents in the JAR file. The generated JAR file <TT>TicTacToe.jar<\/TT> will be placed in the current directory. Because the command used the <TT>v<\/TT> option for verbose output, you would see something similar to this output when you run the command:<br \/>\n<BLOCKQUOTE><PRE>adding: TicTacToe.class (in=3825) (out=2222) (deflated 41%)<br \/>\nadding: audio\/ (in=0) (out=0) (stored 0%)<br \/>\nadding: audio\/beep.au (in=4032) (out=3572) (deflated 11%)<br \/>\nadding: audio\/ding.au (in=2566) (out=2055) (deflated 19%)<br \/>\nadding: audio\/return.au (in=6558) (out=4401) (deflated 32%)<br \/>\nadding: audio\/yahoo1.au (in=7834) (out=6985) (deflated 10%)<br \/>\nadding: audio\/yahoo2.au (in=7463) (out=4607) (deflated 38%)<br \/>\nadding: images\/ (in=0) (out=0) (stored 0%)<br \/>\nadding: images\/cross.gif (in=157) (out=160) (deflated -1%)<br \/>\nadding: images\/not.gif (in=158) (out=161) (deflated -1%)<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P>You can see from this output that the JAR file <TT>TicTacToe.jar<\/TT> is compressed. The Jar tool compresses files by default. You can turn off the compression feature by using the <TT>0<\/TT> (zero) option, so that the command would look like: <\/P><br \/>\n<BLOCKQUOTE><PRE>jar cvf0 TicTacToe.jar TicTacToe.class audio images<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P>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 is a tradeoff in that download time over a network may be longer for larger, uncompressed files. <\/P><br \/>\n<P>The Jar tool will accept arguments that use the wildcard <TT>*<\/TT> symbol. As long as there weren&#8217;t any unwanted files in the <TT>TicTacToe<\/TT> directory, you could have used this alternative command to construct the JAR file: <\/P><br \/>\n<BLOCKQUOTE><PRE>jar cvf TicTacToe.jar *<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P>Though the verbose output doesn&#8217;t indicate it, the Jar tool automatically adds a manifest file to the JAR archive with path name <TT>META-INF\/MANIFEST.MF<\/TT>. See the <A class=TutorialLink href=\"http:\/\/www.strongd.net\/blog\/show\/364\" target=_top>Working with Manifest Files: The Basics<\/A> section for information about manifest files. <\/P><br \/>\n<P>In the above example, the files in the archive retained their relative path names and directory structure. The Jar tool provides the <TT>-C<\/TT> option that you can use to create a JAR file in which the relative paths of the archived files are not preserved. It&#8217;s modeled after TAR&#8217;s <TT>-C<\/TT> option. <\/P><br \/>\n<P>As an example, suppose you wanted to 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 <TT>images<\/TT> and <TT>audio<\/TT> directories: <\/P><br \/>\n<BLOCKQUOTE><PRE>jar cf ImageAudio.jar -C images . -C audio .<br \/>\n<\/PRE><\/BLOCKQUOTE>The <TT>-C&nbsp;images<\/TT> part of this command directs the Jar tool to go to the <TT>images<\/TT> directory, and the <TT>.<\/TT> following <TT>-C&nbsp;images<\/TT> directs the Jar tool to archive all the contents of that directory. The <TT>-C&nbsp;audio&nbsp;.<\/TT> part of the command then does the same with the <TT>audio<\/TT> directory. The resulting JAR file would have this table of contents:<br \/>\n<BLOCKQUOTE><PRE>META-INF\/MANIFEST.MF<br \/>\ncross.gif<br \/>\nnot.gif<br \/>\nbeep.au<br \/>\nding.au<br \/>\nreturn.au<br \/>\nyahoo1.au<br \/>\nyahoo2.au<br \/>\n<\/PRE><\/BLOCKQUOTE>By contrast, suppose that you used a command that did not employ the <TT>-C<\/TT> option:<br \/>\n<BLOCKQUOTE><PRE>jar cf ImageAudio.jar images audio<br \/>\n<\/PRE><\/BLOCKQUOTE>The resulting JAR file would have this table of contents:<br \/>\n<BLOCKQUOTE><PRE>META-INF\/MANIFEST.MF<br \/>\nimages\/cross.gif<br \/>\nimages\/not.gif<br \/>\naudio\/beep.au<br \/>\naudio\/ding.au<br \/>\naudio\/return.au<br \/>\naudio\/yahoo1.au<br \/>\naudio\/yahoo2.au<br \/>\n<\/PRE><\/BLOCKQUOTE><\/BLOCKQUOTE><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The basic format of the command for creating a JAR file is: jar cf jar-file input-file(s) The options and arguments used in this command are: 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. &hellip; <a href=\"https:\/\/www.strongd.net\/?p=365\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Creating a JAR File<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=365"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}