Aug
17
Shelling out in Java – beware, not as simple as it first appears!
Filed Under Computers & Tech on August 17, 2005 at 10:33 pm
Yes, you should of course avoid ever shelling out in Java, but from time to time shelling out becomes the lesser of two evils (the greater usually being writing a native class) and when that happens you need to be aware of some extra complexity that is often over looked and can result in some very strange and hard to diagnose run-time errors!Many people think that all you have to do to shell out in Java is to do something like the following:
Runtime.getRuntime().exec("/usr/local/bin/convert x.png y.jpg");
This works fine if, and only if, you never assume in the rest of your program that this call completed. Or to put it another way, if your script tries to use the output of the program you shelled out to just launching it in that way could result in inconsistent behavior at run time.
Many programmers who are used to shelling out in Perl or the like assume that Java works in the same way and that the program shelling out will wait till the process it shelled out to completes before moving on. In Perl that is a correct assumption, in Java it is not! The shelled out process runs in a separate thread and Java just carries on execution regardless of what is happening with it’s baby process!
If you need Java to wait because you need to manipulate some outcome from the process you spawned or if you need to find out whether is succeeded or failed you need to do a little more. The following is a function I wrote to shell out properly in a project I am working on, it should be a good enough example for you to work out what to do!
static void executeCommand(String cmd) throws Exception{ try { Process p = Runtime.getRuntime().exec(cmd); if(p.waitFor() != 0) { throw new Exception("The command " + cmd + " returned with an error)"); } } catch(Exception e) { // do what ever error handeling you want } }
[…] Following on from my previous post on shelling out in Java. […]