{"id":362,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=362"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Setting an Application's Entry Point","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=362","title":{"rendered":"Setting an Application&#8217;s Entry Point"},"content":{"rendered":"<p><DIV id=PageTitle>Setting an Application&#8217;s Entry Point<\/DIV><br \/>\n<BLOCKQUOTE>If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application&#8217;s entry point. You provide this information with the <CODE>Main-Class<\/CODE> header in the manifest, which has the general form:<br \/>\n<BLOCKQUOTE><PRE>Main-Class: <EM>classname<\/EM><br \/>\n<\/PRE><\/BLOCKQUOTE>The value <EM><CODE>classname<\/CODE><\/EM> is the name of the class that is your application&#8217;s entry point.<br \/>\n<P>Recall that the entry point is a class having a method with signature <CODE>public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)<\/CODE>. <\/P><br \/>\n<P>After you have set the <CODE>Main-Class<\/CODE> header in the manifest, you then run the JAR file using the following form of the <CODE>java<\/CODE> command: <\/P><br \/>\n<BLOCKQUOTE><PRE>java -jar <EM>JAR-name<\/EM><br \/>\n<\/PRE><\/BLOCKQUOTE>The <CODE>main<\/CODE> method of the class specified in the <CODE>Main-Class<\/CODE> header is executed.<br \/>\n<P><\/P><\/BLOCKQUOTE><br \/>\n<H3>An Example<\/H3><br \/>\n<BLOCKQUOTE>We want to execute the <CODE>main<\/CODE> method in the class <CODE>MyClass<\/CODE> in the package <CODE>MyPackage<\/CODE> when we run the JAR file.<br \/>\n<P>We first create a text file named <CODE>Manifest.txt<\/CODE> with the following contents: <\/P><br \/>\n<BLOCKQUOTE><PRE>Main-Class: MyPackage.MyClass<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P><\/P><br \/>\n<BLOCKQUOTE><br \/>\n<HR><br \/>\n<STRONG><FONT color=red>Warning<\/FONT>:<\/STRONG>&nbsp;The text file 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>We then create a JAR file named <CODE>MyJar.jar<\/CODE> by entering the following command:<br \/>\n<BLOCKQUOTE><PRE>jar cfm MyJar.jar Manifest.txt MyPackage\/*.class<br \/>\n<\/PRE><\/BLOCKQUOTE>This creates the JAR file with a manifest with the following contents:<br \/>\n<BLOCKQUOTE><PRE>Manifest-Version: 1.0<br \/>\nCreated-By: 1.6.0 (Sun Microsystems Inc.)<br \/>\nMain-Class: MyPackage.MyClass<br \/>\n<\/PRE><\/BLOCKQUOTE>When you run the JAR file with the following command, the <CODE>main<\/CODE> method of <CODE>MyClass<\/CODE> executes:<br \/>\n<BLOCKQUOTE><PRE>java -jar MyJar.jar<br \/>\n<\/PRE><\/BLOCKQUOTE><\/BLOCKQUOTE><br \/>\n<H3>Setting an Entry Point with the JAR Tool<\/H3><br \/>\n<BLOCKQUOTE>The &#8216;e&#8217; flag (for &#8216;entrypoint&#8217;), introduced in JDK 6, creates or overrides the manifest&#8217;s <CODE>Main-Class<\/CODE> attribute. It can be used while creating or updating a jar file. Use it to specify the application entry point without editing or creating the manifest file. <BR>For example, this command creates <CODE>app.jar<\/CODE> where the <CODE>Main-Class<\/CODE> attribute value in the manifest is set to <CODE>MyApp<\/CODE>:<br \/>\n<BLOCKQUOTE><PRE>jar cfe app.jar MyApp MyApp.class<\/PRE><\/BLOCKQUOTE><br \/>\n<P>You can directly invoke this application by running the following command: <\/P><br \/>\n<BLOCKQUOTE><PRE>java -jar app.jar<\/PRE><\/BLOCKQUOTE><br \/>\n<P>If the entrypoint class name is in a package it may use a &#8216;.&#8217; (dot) character as the delimiter. For example, if <CODE>Main.class<\/CODE> is in a package called <CODE>foo<\/CODE> the entry point can be specified in the following ways: <\/P><br \/>\n<BLOCKQUOTE><PRE>jar cfe Main.jar foo.Main foo\/Main.class<\/PRE><\/BLOCKQUOTE><\/BLOCKQUOTE><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting an Application&#8217;s Entry Point If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application&#8217;s entry point. You provide this information with the Main-Class header in the manifest, which has the general form: Main-Class: classname The value classname is the &hellip; <a href=\"https:\/\/www.strongd.net\/?p=362\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Setting an Application&#8217;s Entry Point<\/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-362","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/362","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=362"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/362\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}