{"id":2005,"date":"2015-02-11T17:30:02","date_gmt":"2015-02-11T23:30:02","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=2005"},"modified":"2015-02-09T17:32:54","modified_gmt":"2015-02-09T23:32:54","slug":"how-to-call-shell-scripts-from-java-properly","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/2005","title":{"rendered":"How to call shell scripts from java properly"},"content":{"rendered":"<figure style=\"width: 150px\" class=\"wp-caption alignleft\"><img decoding=\"async\" class=\"alignleft\" title=\"engraved shell by dd21207\" src=\"http:\/\/www.mooreds.com\/wordpress\/wp-content\/uploads\/2015\/02\/2743580532_576ab9d289_q_shell.jpg\" alt=\"shell photo\" width=\"150\" \/><figcaption class=\"wp-caption-text\"><small>Photo by <a href=\"http:\/\/www.flickr.com\/photos\/58886383@N00\/2743580532\" target=\"_blank\">dd21207<\/a> <a title=\"Attribution License\" href=\"http:\/\/creativecommons.org\/licenses\/by\/2.0\/\" target=\"_blank\" rel=\"nofollow\"><img decoding=\"async\" src=\"http:\/\/www.mooreds.com\/wordpress\/wp-content\/plugins\/wp-inject\/images\/cc.png\" alt=\"\" \/><\/a><\/small><\/figcaption><\/figure>\n<p>This has caused me some grief over the past few weeks, so here&#8217;s some tips on calling bash scripts from java correctly.<\/p>\n<p>Use <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/ProcessBuilder.html\">ProcessBuilder<\/a>. Please.<\/p>\n<p>Make sure you set the <code>redirectErrorStream<\/code> to true, or make other provisions for handling stderr.<\/p>\n<p>I&#8217;ve found that <code>inheritIO<\/code> is useful. Not calling this caused a failure in a bash script that called other java programs.<\/p>\n<p>Make sure your shell scripts have are executable, or that you call them with &#8216;bash scriptname&#8217;. Tarballs preserve permissions, zipfiles do not.<\/p>\n<p>Read the output of the shell script, even if you do nothing with it (but you probably want to log it, if nothing else). If you don&#8217;t, buffers fill up and the process will hang:<\/p>\n<p><code>final InputStreamReader isr = new InputStreamReader(p.getInputStream());<br \/>\nfinal BufferedReader br = new BufferedReader(isr);<br \/>\nString line = null;<br \/>\nStringBuffer sb = new StringBuffer();<br \/>\nwhile ((line = br.readLine()) != null) {<br \/>\nsb.append(line).append(\"\\n\");<br \/>\n}<\/code><\/p>\n<p>Check your exit values, and wait for your shell script to finish: (If looking for speed, or more complex process flow, write it in java.)<\/p>\n<p><code><br \/>\nint exitValue = p.waitFor();<br \/>\nif (exitValue != 0) {<br \/>\n\/\/ error log<br \/>\n}<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This has caused me some grief over the past few weeks, so here&#8217;s some tips on calling bash scripts from java correctly. Use ProcessBuilder. Please. Make sure you set the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[],"class_list":["post-2005","post","type-post","status-publish","format-standard","hentry","category-java","category-programming"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2005"}],"version-history":[{"count":6,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2005\/revisions"}],"predecessor-version":[{"id":2027,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2005\/revisions\/2027"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}