{"id":252,"date":"2013-01-27T15:23:46","date_gmt":"2013-01-27T14:23:46","guid":{"rendered":"http:\/\/benjiweber.co.uk\/blog\/?p=252"},"modified":"2013-01-27T15:23:46","modified_gmt":"2013-01-27T14:23:46","slug":"javascript-shell-scripting-with-nashorn","status":"publish","type":"post","link":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/","title":{"rendered":"JavaScript shell scripting with Nashorn"},"content":{"rendered":"<p class=\"lead\">One of the nice features of <a href=\"https:\/\/blogs.oracle.com\/nashorn\/entry\/welcome_to_the_nashorn_blog\">Nashorn<\/a> is that you can write shell scripts in JavaScript. <\/p>\n<p>It supports #!s, #comments, reading arguments, and everything there&#8217;s a Java library for (Including executing external processes obviously)<\/p>\n<p>Here&#8217;s an example<\/p>\n<pre lang=\"JavaScript\">\r\n#!\/home\/benji\/nashorn7\/bin\/nashorn\r\n#this is a comment\r\n\r\nprint('I am running from ' + __FILE__);\r\n\r\nvar name = arguments.join(' ');\r\nprint('Hello ' + name);\r\nvar runtime = Packages.java.lang.Runtime.getRuntime();\r\nruntime.exec('xmessage hello ' + name);\r\n<\/pre>\n<p><img src=\"http:\/\/benjiweber.co.uk\/nashorn_shellscript.png\" title=\"Shell Output\"\/><\/p>\n<p>Nashorn also comes with an interactive shell &#8220;jjs&#8221;, which is great for trying things out quickly.<\/p>\n<p><img src=\"http:\/\/benjiweber.co.uk\/nashorn_shell.png\"\/><\/p>\n<p>If you want to run the scripts with Java7 instead of Java8 you&#8217;ll need to add Nashorn to the java boot classpath. Simply modify the &#8220;bin\/nashorn&#8221; script and append <\/p>\n<pre>\r\n-J-Xbootclasspath\/a:$NASHORN_HOME\/dist\/nashorn.jar\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One of the nice features of Nashorn is that you can write shell scripts in JavaScript. It supports #!s, #comments, reading arguments, and everything there&#8217;s a Java library for (Including executing external processes obviously) Here&#8217;s an example #!\/home\/benji\/nashorn7\/bin\/nashorn #this is a comment print(&#8216;I am running from &#8216; + __FILE__); var name = arguments.join(&#8216; &#8216;); print(&#8216;Hello&#8230;  <a href=\"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/\" class=\"more-link\" title=\"Read JavaScript shell scripting with Nashorn\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[23,16],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript shell scripting with Nashorn - Benji&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"One of the nice features of Nashorn is that you can write shell scripts in JavaScript. It supports #!s, #comments, reading arguments, and everything there&#8217;s a Java library for (Including executing external processes obviously) Here&#8217;s an example #!\/home\/benji\/nashorn7\/bin\/nashorn #this is a comment print(&#039;I am running from &#039; + __FILE__); var name = arguments.join(&#039; &#039;); print(&#039;Hello... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/\" \/>\n<meta property=\"og:site_name\" content=\"Benji&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2013-01-27T14:23:46+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/benjiweber.co.uk\/nashorn_shellscript.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/\",\"name\":\"Benji&#039;s Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/benjiweber.co.uk\/nashorn_shellscript.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/#webpage\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/\",\"name\":\"JavaScript shell scripting with Nashorn - Benji&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/#primaryimage\"},\"datePublished\":\"2013-01-27T14:23:46+00:00\",\"dateModified\":\"2013-01-27T14:23:46+00:00\",\"author\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/05fb47b31a0b329e1b790074a9b624ef?s=96&d=mm&r=g\",\"caption\":\"benji\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/252"}],"collection":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":5,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":257,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/252\/revisions\/257"}],"wp:attachment":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}