{"id":353,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=353"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Distributing your Application as an executable JAR file","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=353","title":{"rendered":"Distributing your Application as an executable JAR file"},"content":{"rendered":"<p><H2 align=center>Distributing your Application as an executable JAR file<\/H2><br \/>\n<P><BR>A JAR (Java ARchive) is a way of packaging together all of the resources associated with a program (class files, images, sounds, etc.). Putting your program in a JAR allows it to be distributed as a single executable file, saving space and simplifying the download process. The information in this tutorial applies to Java version 1.2 or higher. For more information about JAR files, follow <A href=\"http:\/\/java.sun.com\/tutorial\/jar\">Sun&#8217;s tutorial<\/A>. To learn about <A href=\"http:\/\/www.cs.princeton.edu\/introcs\/85application\/jar\/sign.html\">signing the JAR<\/A> and <A href=\"http:\/\/www.cs.princeton.edu\/introcs\/85application\/jar\/webstart.html\">Java Web Start<\/A>. <\/P><br \/>\n<P><\/P><br \/>\n<P><B>A simple example.<\/B> Let&#8217;s say we wanted to distribute the simple program <A href=\"http:\/\/www.cs.princeton.edu\/introcs\/85application\/jar\/Hello.java\">Hello.java<\/A> as a JAR. First, we create a text file named <TT>Hello.mf<\/TT> which contains: <\/P><br \/>\n<BLOCKQUOTE><PRE>Manifest-Version: 1.0<br \/>\nMain-Class: Hello<br \/>\n<\/PRE><\/BLOCKQUOTE>Then, we create the archive by typing:<br \/>\n<BLOCKQUOTE><PRE>jar cmf Hello.mf Hello.jar Hello.class Hello.java<br \/>\n<\/PRE><\/BLOCKQUOTE>and run it by typing:<br \/>\n<BLOCKQUOTE><PRE>java -jar Hello.jar<br \/>\n<\/PRE><\/BLOCKQUOTE>The file <A href=\"http:\/\/www.cs.princeton.edu\/introcs\/85application\/jar\/Hello.jar\">Hello.jar<\/A> can now be downloaded and executed.<br \/>\n<P><\/P><br \/>\n<P><B>Creating an executable JAR file.<\/B> Here is the general procedure for creating an executable JAR: <\/P><br \/>\n<OL><br \/>\n<P><\/P><br \/>\n<LI>Compile your java code, generating all of the program&#8217;s class files.<br \/>\n<P><\/P><\/LI><br \/>\n<LI>Create a <EM>manifest file<\/EM> containing the following 2 lines:<br \/>\n<BLOCKQUOTE><PRE>Manifest-Version: 1.0<br \/>\nMain-Class: <EM>name of class containing main<\/EM><br \/>\n<\/PRE><\/BLOCKQUOTE>The name of the file should end with the <TT>.mf<\/TT> suffix. It is important that the file ends with a blank line.<br \/>\n<P><\/P><\/LI><br \/>\n<LI>To create the JAR, type the following command:<br \/>\n<BLOCKQUOTE><PRE>jar cmf <I>manifest-file<\/I> <I>jar-file<\/I> <I>input-files<\/I><\/PRE><\/BLOCKQUOTE>The <I>input-files<\/I> must include any class files, images, sounds, etc. that your program uses. Optionally, you can include the program&#8217;s <CODE>.java<\/CODE> files in the JAR. See below for adding directories ot the JAR.<br \/>\n<P><\/P><\/LI><br \/>\n<LI>To view the contents of the JAR, type:<br \/>\n<BLOCKQUOTE><PRE>jar tf <I>jar-file<\/I><\/PRE><\/BLOCKQUOTE><br \/>\n<P><\/P><\/LI><br \/>\n<LI>Execute the application from the command line by typing:<br \/>\n<BLOCKQUOTE><PRE>java -jar <I>jar-file<\/I><\/PRE><\/BLOCKQUOTE>If the application is GUI-based, you can also launch it by double-clicking the JAR file. <\/LI><\/OL><br \/>\n<P><B>Accessing resources in a JAR.<\/B> In general, the first step in accessing a JAR resource involves creating a URL. This might require modifying your program. For example, you can no longer use the following code fragment to read in an image that is stored in a file as follows <\/P><br \/>\n<BLOCKQUOTE><PRE>Image image = Toolkit.getDefaultToolkit().getImage(filename);<br \/>\n<\/PRE><\/BLOCKQUOTE>Instead, create the <TT>URL<\/TT> object using<br \/>\n<BLOCKQUOTE><PRE>URL url = getClass.getResource(filename);<br \/>\nImage image = Toolkit.getDefaultToolkit().getImage(url);<\/PRE><\/BLOCKQUOTE>Or, if the code is in a static method of class <TT>X<\/TT>, then create the URL with<br \/>\n<BLOCKQUOTE><PRE>URL url = X.class.getResource(filename); <\/PRE><\/BLOCKQUOTE>Now, the resource can be accessed the same way, regardless of whether it is in a JAR or the current directory. See the method <CODE>play(filename)<\/CODE> in <A href=\"http:\/\/www.cs.princeton.edu\/introcs\/24inout\/StdDraw.java.html\">StdDraw.java<\/A> for an example involving audio clips, and and the constructor <CODE>In(String s)<\/CODE> in <A href=\"http:\/\/www.cs.princeton.edu\/introcs\/24inout\/In.java.html\">In.java<\/A> for an example involving text files.<br \/>\n<P><B>JAR Subdirectories.<\/B> The JAR format also support storing files in a directory structure. Consider a program <CODE>Sample.java<\/CODE>, which uses the Turtle Graphics interface to display a collection of pictures stored in a subdirectory called <CODE>images<\/CODE>. Our <A href=\"http:\/\/www.cs.princeton.edu\/introcs\/85application\/jar\/sample\">working directory<\/A> looks like: <\/P><br \/>\n<P><BR><IMG height=490 src=\"http:\/\/www.cs.princeton.edu\/introcs\/85application\/jar\/chart2.gif\"><BR><\/P><br \/>\n<P>The Manifest should read: <\/P><br \/>\n<BLOCKQUOTE><PRE>Manifest-Version: 1.0<br \/>\nMain-Class: Sample<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P>To create the JAR, type: <\/P><br \/>\n<BLOCKQUOTE><PRE>jar cmf Sample.mf Sample.jar Sample.class Turtle.class Sample.java Turtle.java images<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P>The contents listing appears as: <\/P><br \/>\n<BLOCKQUOTE><PRE>META-INF\/<br \/>\nMETA-INF\/MANIFEST.MF<br \/>\nSample.class<br \/>\nTurtle.class<br \/>\nSample.java<br \/>\nTurtle.java<br \/>\nimages\/<br \/>\nimages\/image1.gif<br \/>\nimages\/image2.gif<br \/>\nimages\/image3.gif<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P>Notice that the directory structure is still preserved (the META-INF directory is created to hold the manifest and other general information about the JAR). <\/P><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Distributing your Application as an executable JAR file A JAR (Java ARchive) is a way of packaging together all of the resources associated with a program (class files, images, sounds, etc.). Putting your program in a JAR allows it to be distributed as a single executable file, saving space and simplifying the download process. The &hellip; <a href=\"https:\/\/www.strongd.net\/?p=353\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Distributing your Application as an executable 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-353","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/353","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=353"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}