{"id":4859,"date":"2014-03-30T16:09:41","date_gmt":"2014-03-30T16:09:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-kill-runtime-exec-collection-of-common-programming-errors\/"},"modified":"2014-03-30T16:09:41","modified_gmt":"2014-03-30T16:09:41","slug":"how-to-kill-runtime-exec-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-kill-runtime-exec-collection-of-common-programming-errors\/","title":{"rendered":"how to kill runtime.exec?-Collection of common programming errors"},"content":{"rendered":"<p>For this, you can employ the features present in java.util.concurrent<\/p>\n<p>In your existing class containing your UI, you have to add something similar to this:<\/p>\n<pre><code>\/\/class variable to store the future of your task\nprivate Future taskFuture = null;\n\n\/\/to be called from button \"Start\" action handler\npublic void actionStart() {\n\n  \/\/don't double start, if there is one already running\n  if(taskFuture == null || taskFuture.isDone()) {\n\n    \/\/create the new runnable instance, with the proper commands to execute\n    MyShellExecutor ex = new MyShellExecutor(new String[] { \"sh\",testPath + \"\/install.sh\", cmd, \"&amp;\" });\n\n    \/\/we only need one additional Thread now, but this part can be tailored to fit different needs\n    ExecutorService newThreadExecutor = Executors.newSingleThreadExecutor();\n\n    \/\/start the execution of the task, which will start execution of the shell command\n    taskFuture = newThreadExecutor.submit(ex);\n  }\n}\n\n\/\/to be called from button \"Stop\" action handler\npublic void actionStop() {\n  \/\/if not already done, or cancelled, cancel it\n  if(taskFuture !=null &amp;&amp; !taskFuture.isDone()) {\n    taskFuture.cancel(true);\n  }\n}\n<\/code><\/pre>\n<p>The main component doing the job is the <code>Runnable<\/code> which I named <code>MyShellExecutor<\/code>, and looks like this:<\/p>\n<pre><code>public class MyShellExecutor implements Runnable {\n\n    \/\/stores the command to be executed\n    private final String[] toExecute;\n\n    public MyShellExecutor(String[] toExecute) {\n        this.toExecute=toExecute;\n    }\n\n    public void run() {\n        Runtime runtime = Runtime.getRuntime();\n        Process process = null;\n\n        try {\n            process = runtime.exec(toExecute);\n\n            int exitValue = process.waitFor();\n            System.out.println(\"exit value: \" + exitValue);\n            BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));\n\n            String line = \"\";\n            while ((line = buf.readLine()) != null) {\n                System.out.println(\"exec response: \" + line);\n                \/\/do whatever you need to do\n            }\n\n        } catch (InterruptedException e) {\n            \/\/thread was interrupted.\n            if(process!=null) { process.destroy(); }\n            \/\/reset interrupted flag\n            Thread.currentThread().interrupt();\n\n        } catch (Exception e) {\n            \/\/an other error occurred\n            if(process!=null) { process.destroy(); }\n        }\n\n    }\n}\n<\/code><\/pre>\n<p>Note: when running time consuming operations, be sure not to do it on the UI thread. That just blocks the user, and doesn&#8217;t provide a nice user experience. Always do anzthing that might make the user wait on a different Thread.<\/p>\n<p>Recommended reading: Java Concurrency In Practice<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For this, you can employ the features present in java.util.concurrent In your existing class containing your UI, you have to add something similar to this: \/\/class variable to store the future of your task private Future taskFuture = null; \/\/to be called from button &#8220;Start&#8221; action handler public void actionStart() { \/\/don&#8217;t double start, if [&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-4859","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4859","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=4859"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4859\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}