{"id":1552,"date":"2015-11-18T15:14:07","date_gmt":"2015-11-18T14:14:07","guid":{"rendered":"http:\/\/marcoratto.it\/blog\/?page_id=1552"},"modified":"2015-11-18T17:48:48","modified_gmt":"2015-11-18T16:48:48","slug":"javashebang","status":"publish","type":"page","link":"https:\/\/marcoratto.it\/blog\/javashebang\/","title":{"rendered":"javashebang"},"content":{"rendered":"<p>First of all: what is shenbag ?<br \/>\nFrom <a href=\"https:\/\/en.wikipedia.org\/wiki\/Shebang_(Unix)\" target=\"_blank\">Wikipedia Shebang_(Unix)<\/a>:<br \/>\n<em>In computing, a <b>shebang<\/b> (also called a sha-bang, hashbang, pound-bang, or hash-pling), is the character sequence consisting of the characters number sign and exclamation mark (that is, &#8220;<strong>#!<\/strong>&#8220;) at the beginning of a script. Some examples are the following:<\/em><br \/>\n#!\/bin\/bash<br \/>\n#!\/bin\/ksh<br \/>\n#!\/bin\/sh<br \/>\n#!\/usr\/bin\/env php<br \/>\n#!\/usr\/bin\/expect -f<br \/>\n#!\/usr\/bin\/ksh<br \/>\n#!\/usr\/local\/bin\/expect<\/p>\n<p>For using\u00a0Java language on scripting (Linux, UNIX, etc.) you have to:<\/p>\n<ul>\n<li><strong>Write the java code using an IDE (Eclipse, for example)<\/strong><\/li>\n<li><strong>Compile the class<\/strong><\/li>\n<li><strong>Create a jar package (not mandatory but it is better to create)<\/strong><\/li>\n<li><strong>Create a shell script *.sh for running the Java class<\/strong><\/li>\n<li><strong>Distribute all with a zip or tar.gz file<\/strong><\/li>\n<\/ul>\n<p>My idea\u00a0is <em>&#8220;How can I simplify this ?&#8221;<\/em>.\u00a0My requirements are the following:<\/p>\n<ul>\n<li><strong>Write the java code directly in a file-script<\/strong><\/li>\n<li><strong>Compile it if the script changed<\/strong><\/li>\n<li><strong>Compile it if the java version\u00a0changed<\/strong><\/li>\n<li><strong>Run it<\/strong><\/li>\n<li><strong>Read parameters from the command line<\/strong><\/li>\n<li><strong>Add external libraries<\/strong><\/li>\n<\/ul>\n<p>This is an example of my javashebang:<\/p>\n<blockquote><p>#!\/bin\/javashebang<br \/>\n#START_JSH<br \/>\n#JAVA_HOME=${JAVA_HOME}<br \/>\n#JAVA_OPTS=-Xms256m -Xmx1024m<br \/>\n#END_JSH<\/p>\n<p>public class HelloWorld {<\/p>\n<p>public static void main(String[] args) {<br \/>\nif (args.length == 0) {<br \/>\nSystem.err.println(&#8220;Parameters: VALUES&#8221;);<br \/>\nSystem.exit(1);<br \/>\n}<br \/>\nSystem.out.println(&#8220;There are &#8221; + args.length + &#8221; parameters:&#8221;);<br \/>\nfor (int j=0; j&lt;args.length; j++) {<br \/>\nSystem.out.println(&#8220;args[&#8221; + j + &#8220;]=&#8221; + args[j]);<br \/>\n}<br \/>\nSystem.exit(0);<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n<p>The output is the following:<\/p>\n<p><strong>.\/HelloWorld.jsh<\/strong><br \/>\nParameters: VALUES<\/p>\n<p>The folder contains the following files:<\/p>\n<ul>\n<li><strong>HelloWorld.jsh<\/strong> -&gt; the source script<\/li>\n<li><strong>HelloWorld.java<\/strong> -&gt; the java class extracted from the script<\/li>\n<li><strong>HelloWorld.class<\/strong> -&gt; the java class compiled<\/li>\n<\/ul>\n<p><strong>Requirements:<\/strong><\/p>\n<ul>\n<li>A UNIX System (Linux, AIX, etc)<\/li>\n<li>A JDK installed (not JRE): OpenJDK, Oracle JDK, IBM JDK, etc.<\/li>\n<\/ul>\n<p><strong>Features:<\/strong><\/p>\n<ul>\n<li>Option &#8220;-d&#8221; for debugging<\/li>\n<li>Option &#8220;-v&#8221; for\u00a0verbose output<\/li>\n<li>Option &#8220;-f&#8221; for\u00a0forcing to compile<\/li>\n<li>Extension of the script\u00a0is &#8220;*.jsh&#8221; (suggested, not mandatory)<\/li>\n<li>The script MUST TO have the same name as the java class (mandatory)<\/li>\n<li>The java class MUST TO have the static method main (mandatory)<\/li>\n<li>Header of the file &#8220;#JAVA_OPT&#8221; for managing the options on java command line tool<\/li>\n<li>Header of the file &#8220;#JAVAC_OPT&#8221; for managing the options on javac command line tool<\/li>\n<li>Header of the file &#8220;#JAVA_HOME&#8221; for\u00a0running java, javac and javap command line tools<\/li>\n<li>Header of the file &#8220;#JAVA_CLASSAPTH&#8221; for managing the options on javac and java command line tools<\/li>\n<li>Test on Linux (Ubuntu) and AIX<\/li>\n<\/ul>\n<p><strong>Setup<\/strong><\/p>\n<ol>\n<li>Download the code of javashebang from my <a href=\"https:\/\/github.com\/marcoratto\/marcoratto\/tree\/master\/javashebang\" target=\"_blank\">git repository<\/a>.<\/li>\n<li>You can download some examples from this\u00a0<a href=\"https:\/\/github.com\/marcoratto\/marcoratto\/tree\/master\/javashebang\/examples\" target=\"_blank\">git examples<\/a>.<\/li>\n<li>Copy the file on \/bin\/javashebang<\/li>\n<li>sudo chmod 755\u00a0\/bin\/javashebang<\/li>\n<li>Use it<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>First of all: what is shenbag ? From Wikipedia Shebang_(Unix): In computing, a shebang (also called a sha-bang, hashbang, pound-bang, or hash-pling), is the character sequence consisting of the characters number sign and exclamation mark (that is, &#8220;#!&#8220;) at the beginning of a script. Some examples are the following: #!\/bin\/bash #!\/bin\/ksh #!\/bin\/sh #!\/usr\/bin\/env php #!\/usr\/bin\/expect [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1552","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/marcoratto.it\/blog\/wp-json\/wp\/v2\/pages\/1552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/marcoratto.it\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/marcoratto.it\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/marcoratto.it\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/marcoratto.it\/blog\/wp-json\/wp\/v2\/comments?post=1552"}],"version-history":[{"count":8,"href":"https:\/\/marcoratto.it\/blog\/wp-json\/wp\/v2\/pages\/1552\/revisions"}],"predecessor-version":[{"id":1560,"href":"https:\/\/marcoratto.it\/blog\/wp-json\/wp\/v2\/pages\/1552\/revisions\/1560"}],"wp:attachment":[{"href":"https:\/\/marcoratto.it\/blog\/wp-json\/wp\/v2\/media?parent=1552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}