{"id":3635,"date":"2014-03-29T07:18:44","date_gmt":"2014-03-29T07:18:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/need-sample-java-code-to-run-a-shellscript-collection-of-common-programming-errors\/"},"modified":"2014-03-29T07:18:44","modified_gmt":"2014-03-29T07:18:44","slug":"need-sample-java-code-to-run-a-shellscript-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/need-sample-java-code-to-run-a-shellscript-collection-of-common-programming-errors\/","title":{"rendered":"Need sample Java code to run a shellscript-Collection of common programming errors"},"content":{"rendered":"<p>You need Runtime.getRuntime().exec(&#8230;). See a very extensive example (don&#8217;t forget to read the first three pages).<\/p>\n<p>Keep in mind that Runtime.exec is <em>not a shell<\/em>; if you wish to execute a shell script your command line would look like<\/p>\n<pre><code>\/bin\/bash scriptname\n<\/code><\/pre>\n<p>That is, the shell binary you need is fully qualified (although I suspect that \/bin is always in the path). You can not assume that if<\/p>\n<pre><code>myshell&gt; foo.sh\n<\/code><\/pre>\n<p>runs,<\/p>\n<pre><code>Runtime.getRuntime.exec(\"foo.sh\");\n<\/code><\/pre>\n<p>also runs as you are already in a running shell in the first example, but not in the Runtime.exec.<\/p>\n<p>A tested example (Works on My Linux Machine(TM)), mosly cut-and-past from the previously mentioned article:<\/p>\n<pre><code>import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.InputStreamReader;\npublic class ShellScriptExecutor {\n\n    static class StreamGobbler extends Thread {\n        InputStream is;\n\n        String type;\n\n        StreamGobbler(InputStream is, String type) {\n                this.is = is;\n                this.type = type;\n        }\n\n        public void run() {\n                try {\n                        InputStreamReader isr = new InputStreamReader(is);\n                        BufferedReader br = new BufferedReader(isr);\n                        String line = null;\n                        while ((line = br.readLine()) != null)\n                                System.out.println(type + \"&gt;\" + line);\n                } catch (IOException ioe) {\n                        ioe.printStackTrace();\n                }\n        }\n    }\n\n\n    public static void main(String[] args) {\n        if (args.length &lt; 1) {\n                System.out.println(\"USAGE: java ShellScriptExecutor script\");\n                System.exit(1);\n        }\n\n        try {\n                String osName = System.getProperty(\"os.name\");\n                String[] cmd = new String[2];\n                cmd[0] = \"\/bin\/sh\"; \/\/ should exist on all POSIX systems\n                cmd[1] = args[0];\n\n                Runtime rt = Runtime.getRuntime();\n                System.out.println(\"Execing \" + cmd[0] + \" \" + cmd[1] );\n                Process proc = rt.exec(cmd);\n                \/\/ any error message?\n                StreamGobbler errorGobbler = new StreamGobbler(proc\n                                .getErrorStream(), \"ERROR\");\n\n                \/\/ any output?\n                StreamGobbler outputGobbler = new StreamGobbler(proc\n                                .getInputStream(), \"OUTPUT\");\n\n                \/\/ kick them off\n                errorGobbler.start();\n                outputGobbler.start();\n\n                \/\/ any error???\n                int exitVal = proc.waitFor();\n                System.out.println(\"ExitValue: \" + exitVal);\n        } catch (Throwable t) {\n                t.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You need Runtime.getRuntime().exec(&#8230;). See a very extensive example (don&#8217;t forget to read the first three pages). Keep in mind that Runtime.exec is not a shell; if you wish to execute a shell script your command line would look like \/bin\/bash scriptname That is, the shell binary you need is fully qualified (although I suspect that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3635","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3635"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3635\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}