{"id":1006,"date":"2011-05-13T20:42:38","date_gmt":"2011-05-13T18:42:38","guid":{"rendered":"http:\/\/raftaman.net\/?p=1006"},"modified":"2021-05-15T11:46:40","modified_gmt":"2021-05-15T09:46:40","slug":"multiple-main-methods-in-a-single-jar-archive","status":"publish","type":"post","link":"https:\/\/possiblelossofprecision.net\/?p=1006","title":{"rendered":"Multiple main methods in a single jar archive"},"content":{"rendered":"<p>A <a href=\"http:\/\/en.wikipedia.org\/wiki\/JAR_%28file_format%29\">Java archive file<\/a> usually holds a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Manifest_file\">manifest file<\/a> that specifies the main class to start when you invoke <code>java -jar jarfile<\/code>. It&#8217;s something similar to this:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\nMain-Class: com.yourcom.package.MyClass\r\n<\/pre>\n<p>When you distribute your jar file, a user can simply execute it (possibly even with a double click) without knowing anything about your package structure or location of the main class.<\/p>\n<p>But what if you want to <strong>execute a different main method<\/strong> in another class inside your jar file, maybe for testing purposes? Well, you don&#8217;t have to hack the manifest and re-package. Simply do<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\njava -cp jarfile com.yourcom.package.MyClass\r\n<\/pre>\n<p>and the main method inside <code>MyClass<\/code> will be executed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Java archive file usually holds a manifest file that specifies the main class to start when you invoke java -jar jarfile. It&#8217;s something similar to this: Main-Class: com.yourcom.package.MyClass When you distribute your jar file, a user can simply execute it (possibly even with a double click) without knowing anything about your package structure or location of the main class&#8230;. <a href=\"https:\/\/possiblelossofprecision.net\/?p=1006\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4],"class_list":["post-1006","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java"],"_links":{"self":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/1006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1006"}],"version-history":[{"count":5,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/1006\/revisions"}],"predecessor-version":[{"id":1011,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=\/wp\/v2\/posts\/1006\/revisions\/1011"}],"wp:attachment":[{"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/possiblelossofprecision.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}