{"id":1339,"date":"2015-09-09T15:49:38","date_gmt":"2015-09-09T13:49:38","guid":{"rendered":"http:\/\/blog.rabahi.net\/?page_id=1339"},"modified":"2017-05-11T10:30:18","modified_gmt":"2017-05-11T08:30:18","slug":"java-processfactory","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=1339","title":{"rendered":"Java &#8211; Process"},"content":{"rendered":"<p>In some case, we want to run some external commands.<br \/>\nHere i will show you how to do it.<\/p>\n<p>For example we want to run the command: ls -la<\/p>\n<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Set_the_command_we_want_to_run\"><span class=\"toc_number toc_depth_1\">1<\/span> Set the command we want to run<\/a><\/li><li><a href=\"#Start_ProcessBuilder\"><span class=\"toc_number toc_depth_1\">2<\/span> Start ProcessBuilder<\/a><\/li><li><a href=\"#Set_StdOut_and_StdErr\"><span class=\"toc_number toc_depth_1\">3<\/span> Set StdOut and StdErr<\/a><\/li><li><a href=\"#Run_process\"><span class=\"toc_number toc_depth_1\">4<\/span> Run process<\/a><\/li><li><a href=\"#Now_do_something_with_exitCode\"><span class=\"toc_number toc_depth_1\">5<\/span> Now do something with exitCode<\/a><\/li><\/ul><\/div>\n<h1><span id=\"Set_the_command_we_want_to_run\">Set the command we want to run<\/span><\/h1>\n<pre lang=\"java\">\r\n\/\/ Set the command to run\r\nString command=\"ls -la\";\r\n<\/pre>\n<h1><span id=\"Start_ProcessBuilder\">Start ProcessBuilder<\/span><\/h1>\n<pre lang=\"java\">\r\n\/\/ Initialize process builder.\r\nProcessBuilder processBuilder = new ProcessBuilder( command );\r\n\r\n\/\/ And start it\r\nProcess process = processBuilder.start();\r\n<\/pre>\n<h1><span id=\"Set_StdOut_and_StdErr\">Set StdOut and StdErr<\/span><\/h1>\n<pre lang=\"java\">\r\n\/\/ Redirect StdOut to LOGGER with info level\r\nfinal StreamPumper stdOutPumper = new StreamPumper( process.getInputStream(), LOGGER::info);\r\nstdOutPumper.start();\r\n\r\n\/\/ Redirect StdErr to LOGGER with error level\r\nfinal StreamPumper stdErrPumper = new StreamPumper( process.getErrorStream(), LOGGER::error);\r\nstdErrPumper.start();\r\n<\/pre>\n<h1><span id=\"Run_process\">Run process<\/span><\/h1>\n<p>You can exec process :<\/p>\n<pre lang=\"java\">\r\n\/\/ Exec process and get exit code.\r\nint exitCode = process.waitFor();\r\n<\/pre>\n<p>You can fork process :<\/p>\n<pre lang=\"java\">\r\n\/\/ Fork process and get exit code.\r\nint exitCode = process.wait();\r\n<\/pre>\n<h1><span id=\"Now_do_something_with_exitCode\">Now do something with exitCode<\/span><\/h1>\n<pre lang=\"java\">\r\nif(exitCode != 0) {\r\n  LOGGER.debug(\"Process exit with code != O\");\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In some case, we want to run some external commands. Here i will show you how to do it. For example we want to run the command: ls -la Contents1 Set the command we want to run2 Start ProcessBuilder3 Set StdOut and StdErr4 Run process5 Now do something with exitCode Set the command we want [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1538,"menu_order":12,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1339","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1339"}],"version-history":[{"count":7,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1339\/revisions"}],"predecessor-version":[{"id":1963,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1339\/revisions\/1963"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1538"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}