{"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 decoding=\"async\" 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 decoding=\"async\" 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":{"footnotes":""},"categories":[8],"tags":[23,16],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript shell scripting with Nashorn - Benji&#039;s Blog<\/title>\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=\"author\" content=\"benji\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"benji\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/01\\\/27\\\/javascript-shell-scripting-with-nashorn\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/01\\\/27\\\/javascript-shell-scripting-with-nashorn\\\/\"},\"author\":{\"name\":\"benji\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#\\\/schema\\\/person\\\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"headline\":\"JavaScript shell scripting with Nashorn\",\"datePublished\":\"2013-01-27T14:23:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/01\\\/27\\\/javascript-shell-scripting-with-nashorn\\\/\"},\"wordCount\":91,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/01\\\/27\\\/javascript-shell-scripting-with-nashorn\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/benjiweber.co.uk\\\/nashorn_shellscript.png\",\"keywords\":[\"Java\",\"JavaScript\"],\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/01\\\/27\\\/javascript-shell-scripting-with-nashorn\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/01\\\/27\\\/javascript-shell-scripting-with-nashorn\\\/\",\"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\"},\"image\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/01\\\/27\\\/javascript-shell-scripting-with-nashorn\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/benjiweber.co.uk\\\/nashorn_shellscript.png\",\"datePublished\":\"2013-01-27T14:23:46+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#\\\/schema\\\/person\\\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/01\\\/27\\\/javascript-shell-scripting-with-nashorn\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/01\\\/27\\\/javascript-shell-scripting-with-nashorn\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/01\\\/27\\\/javascript-shell-scripting-with-nashorn\\\/#primaryimage\",\"url\":\"http:\\\/\\\/benjiweber.co.uk\\\/nashorn_shellscript.png\",\"contentUrl\":\"http:\\\/\\\/benjiweber.co.uk\\\/nashorn_shellscript.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/01\\\/27\\\/javascript-shell-scripting-with-nashorn\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript shell scripting with Nashorn\"}]},{\"@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\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#\\\/schema\\\/person\\\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"caption\":\"benji\"},\"sameAs\":[\"http:\\\/\\\/benjiweber.co.uk\"],\"url\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/author\\\/benji\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript shell scripting with Nashorn - Benji&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript shell scripting with Nashorn - Benji&#039;s Blog","og_description":"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('I am running from ' + __FILE__); var name = arguments.join(' '); print('Hello... Read more &raquo;","og_url":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/","og_site_name":"Benji&#039;s Blog","article_published_time":"2013-01-27T14:23:46+00:00","og_image":[{"url":"http:\/\/benjiweber.co.uk\/nashorn_shellscript.png","type":"","width":"","height":""}],"author":"benji","twitter_card":"summary_large_image","twitter_misc":{"Written by":"benji","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/#article","isPartOf":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/"},"author":{"name":"benji","@id":"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09"},"headline":"JavaScript shell scripting with Nashorn","datePublished":"2013-01-27T14:23:46+00:00","mainEntityOfPage":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/"},"wordCount":91,"commentCount":0,"image":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/#primaryimage"},"thumbnailUrl":"http:\/\/benjiweber.co.uk\/nashorn_shellscript.png","keywords":["Java","JavaScript"],"articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/","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"},"image":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/#primaryimage"},"thumbnailUrl":"http:\/\/benjiweber.co.uk\/nashorn_shellscript.png","datePublished":"2013-01-27T14:23:46+00:00","author":{"@id":"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09"},"breadcrumb":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/#primaryimage","url":"http:\/\/benjiweber.co.uk\/nashorn_shellscript.png","contentUrl":"http:\/\/benjiweber.co.uk\/nashorn_shellscript.png"},{"@type":"BreadcrumbList","@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/01\/27\/javascript-shell-scripting-with-nashorn\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/benjiweber.co.uk\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript shell scripting with Nashorn"}]},{"@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":{"@type":"EntryPoint","urlTemplate":"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09","name":"benji","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","caption":"benji"},"sameAs":["http:\/\/benjiweber.co.uk"],"url":"https:\/\/benjiweber.co.uk\/blog\/author\/benji\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"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}]}}