{"id":5587,"date":"2022-03-21T20:46:29","date_gmt":"2022-03-22T03:46:29","guid":{"rendered":"https:\/\/blackcap.name\/blog\/new\/?p=5587"},"modified":"2022-04-03T19:09:36","modified_gmt":"2022-04-04T02:09:36","slug":"creating-an-executable-jar-for-unix-and-linux","status":"publish","type":"post","link":"https:\/\/blackcap.name\/blog\/new\/?p=5587","title":{"rendered":"Creating an Executable JAR for Unix and Linux"},"content":{"rendered":"\n<p>One of the annoying things about any JVM language is that to run the result of compiling your code, you have to type something like:<\/p>\n\n\n<pre><code>java -cp somefile.jar domain.name.name2.SomeClass arg1 arg2 \u2026<\/code><\/pre>\n\n\n<p>Or at best:<\/p>\n\n\n\n<p><code>java -jar somefile.jar arg1 arg2 \u2026<\/code><\/p>\n\n\n\n<p>Wouldn&#8217;t it be great if you could just type the command name followed by arguments, like you can do with a compiled C or C++ program? The normal way to do this is to write a shell script and make it executable, but this is a tad clunky (now there are two files, the shell script and the JAR that it invokes). It would be nicer to have just a single executable.<\/p>\n\n\n\n<p>Well, you can!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"#!\/usr\/bin\/env java -jar\" &gt; somename\ncat somefile.jar &gt;&gt; somename\nchmod +x somename<\/code><\/pre>\n\n\n\n<p>And that is it! You now have an executable binary that is an archive of Java bytecode instead of native machine code. (Of course, it requires a suitable java interpreter to exist on your PATH.)<\/p>\n\n\n\n<p>Best of all, while all of this sounds hackish, it is <em>not<\/em> just luck that a JAR file with some leading junk tacked on to it is still treated as a valid JAR file. No, this is basically <em>guaranteed<\/em> to work. You see, JAR files contain their header data <em>at the end<\/em>, not the beginning, and Java simply ignores all data earlier than what is described in the header.<\/p>\n\n\n\n<p>And since the Macintosh is just a UNIX system under the hood, this trick works for Macs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the annoying things about any JVM language is that to run the result of compiling your code, you have to type something like: java -cp somefile.jar domain.name.name2.SomeClass arg1 arg2 \u2026 Or at best: java -jar somefile.jar arg1 arg2 \u2026 Wouldn&#8217;t it be great if you could just type the command name followed by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-5587","post","type-post","status-publish","format-standard","hentry","category-computers"],"_links":{"self":[{"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/posts\/5587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5587"}],"version-history":[{"count":4,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/posts\/5587\/revisions"}],"predecessor-version":[{"id":5636,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=\/wp\/v2\/posts\/5587\/revisions\/5636"}],"wp:attachment":[{"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blackcap.name\/blog\/new\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}