{"id":369,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=369"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Running JAR-Packaged Software","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=369","title":{"rendered":"Running JAR-Packaged Software"},"content":{"rendered":"<p><DIV id=PageTitle>Running JAR-Packaged Software<\/DIV><br \/>\n<BLOCKQUOTE>Now that you&#8217;ve learned how to create JAR files, how do you actually run the code that you&#8217;ve packaged? Consider these three scenarios:<br \/>\n<UL><br \/>\n<LI>Your JAR file contains an applet that is to be run inside a browser. <\/LI><br \/>\n<LI>Your JAR file contains an application that is to be invoked from the command line. <\/LI><br \/>\n<LI>Your JAR file contains code that you want to use as an extension. <\/LI><\/UL><br \/>\n<P>This section will cover the first two situations. A separate trail in the tutorial on the <A class=TutorialLink href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/ext\/index.html\" target=_top>extension mechanism<\/A> covers the use of JAR files as extensions. <\/P><\/BLOCKQUOTE><br \/>\n<H3>Applets Packaged in JAR Files<\/H3><br \/>\n<BLOCKQUOTE>To invoke any applet from an HTML file for running inside a browser, you need to use the APPLET tag. For more information, see the <A class=TutorialLink href=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/deployment\/applet\/index.html\" target=_top>Applets<\/A> lesson. If the applet is bundled as a JAR file, the only thing you need to do differently is to use the <EM>ARCHIVE<\/EM> parameter to specify the relative path to the JAR file.<br \/>\n<P>As an example, let&#8217;s use (again!) the TicTacToe demo applet that ships with the Java<FONT size=-2><SUP>TM<\/SUP><\/FONT> Development Kit. The APPLET tag in the HTML file that calls the demo looks like this: <\/P><br \/>\n<BLOCKQUOTE><PRE>&lt;applet code=TicTacToe.class<br \/>\n        width=120 height=120&gt;<br \/>\n&lt;\/applet&gt;<br \/>\n<\/PRE><\/BLOCKQUOTE>If the TicTacToe demo were packaged in a JAR file named <TT>TicTacToe.jar<\/TT>, you could modify the APPLET tag with the simple addition of an ARCHIVE parameter:<br \/>\n<BLOCKQUOTE><PRE>&lt;applet code=TicTacToe.class<br \/>\n        archive=&#8221;TicTacToe.jar&#8221;<br \/>\n        width=120 height=120&gt;<br \/>\n&lt;\/applet&gt;<br \/>\n<\/PRE><\/BLOCKQUOTE>The ARCHIVE parameter specifies the relative path to the JAR file that contains <TT>TicTacToe.class<\/TT>. This example assumes that the JAR file and the HTML file are in the same directory. If they&#8217;re not, you would need to include the JAR file&#8217;s relative path in the ARCHIVE parameter&#8217;s value. For example, if the JAR file was one directory below the HTML file in a directory called <TT>applets<\/TT>, the APPLET tag would look like this:<br \/>\n<BLOCKQUOTE><PRE>&lt;applet code=TicTacToe.class<br \/>\n        archive=&#8221;applets\/TicTacToe.jar&#8221;<br \/>\n        width=120 height=120&gt;<br \/>\n&lt;\/applet&gt;<br \/>\n<\/PRE><\/BLOCKQUOTE><\/BLOCKQUOTE><A name=1.2><\/A><br \/>\n<H3>JAR Files as Applications<\/H3><br \/>\n<BLOCKQUOTE>You can run JAR-packaged applications with the Java interpreter. The basic command is:<br \/>\n<BLOCKQUOTE><PRE>java -jar <I>jar-file<\/I><br \/>\n<\/PRE><\/BLOCKQUOTE>The <TT>-jar<\/TT> flag tells the interpreter that the application is packaged in the JAR file format. You can only specify one JAR file, which must contain all the application-specific code.<br \/>\n<P>Before you execute this command make sure the runtime environment has an information of which class within the JAR file is the application&#8217;s entry point. <\/P><br \/>\n<P>To indicate which class is the application&#8217;s entry point, you must add a <TT>Main-Class<\/TT> header to the JAR file&#8217;s manifest. The header takes the form: <\/P><br \/>\n<BLOCKQUOTE><PRE>Main-Class: <I>classname<\/I><br \/>\n<\/PRE><\/BLOCKQUOTE>The header&#8217;s value, <TT>classname<\/TT>, is the name of the class that&#8217;s the application&#8217;s entry point.<br \/>\n<P>For more information, see the <A class=TutorialLink href=\"http:\/\/www.strongd.net\/blog\/show\/362\" target=_top>Setting an Application&#8217;s Entry Point<\/A> section. <\/P><br \/>\n<P>When the <TT>Main-Class<\/TT> is set in the manifest file, you can run the application from the command line: <\/P><br \/>\n<BLOCKQUOTE><PRE>java -jar app.jar<br \/>\n<\/PRE><\/BLOCKQUOTE><\/BLOCKQUOTE><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Running JAR-Packaged Software Now that you&#8217;ve learned how to create JAR files, how do you actually run the code that you&#8217;ve packaged? Consider these three scenarios: Your JAR file contains an applet that is to be run inside a browser. Your JAR file contains an application that is to be invoked from the command line. &hellip; <a href=\"https:\/\/www.strongd.net\/?p=369\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Running JAR-Packaged Software<\/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-369","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/369","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=369"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/369\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}