{"id":4850,"date":"2011-12-29T10:40:51","date_gmt":"2011-12-29T05:10:51","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4850"},"modified":"2016-12-19T15:04:57","modified_gmt":"2016-12-19T09:34:57","slug":"executing-linux-shell-command-using-processbuilder","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/executing-linux-shell-command-using-processbuilder\/","title":{"rendered":"Executing linux shell command using ProcessBuilder"},"content":{"rendered":"<p>We often need to execute linux shell command from our groovy code.  What I had to do in my project was to resize the image saved on file system.<\/p>\n<p>I was doing something like :<\/p>\n<p>[java]String command = &quot;convert -adaptive-resize ${width}x${height}! ${sourceFilePath} ${destinationFilePath}&quot;<br \/>\nProcess process=command.execute()<br \/>\nprocess.waitFor()<br \/>\n[\/java]<\/p>\n<p>The above code makes a new image file with specified width and height of image. The problem I encountered was that the above code doesnot work for files having spaces in their name e.g. file name &#8220;new house.jpg&#8221;<br \/>\n<br \/>\nThen by googling I found ProcessBuilder which is a utility class that interact with OS from java program and create OS processes.<\/p>\n<p>[java]ProcessBuilder pb = new ProcessBuilder(&quot;convert&quot;, &quot;-adaptive-resize&quot;, &quot;${width}x${height}!&quot;, sourceFilePath, destinationFilePath);<br \/>\nProcess p = pb.start()<br \/>\np.waitFor()<br \/>\n[\/java]<br \/>\nIt works seemlessly!!!<br \/>\n&#8211;<br \/>\n@Divya Setia<br \/>\ndivya@intelligrape.com<br \/>\n<a href=\"http:\/\/www.tothenew.com\">http:\/\/www.intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We often need to execute linux shell command from our groovy code. What I had to do in my project was to resize the image saved on file system. I was doing something like : [java]String command = &quot;convert -adaptive-resize ${width}x${height}! ${sourceFilePath} ${destinationFilePath}&quot; Process process=command.execute() process.waitFor() [\/java] The above code makes a new image file [&hellip;]<\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":10},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4850"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4850"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4850\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}