AmanKing

Runtime.exec()Control PanelChange LogBrowse PagesSearch?

Runtime.exec()

Thank God for Runtime.exec()!

There are so many great utilities and OS commands out there that just don't have a Java version (at least not one that comes close enough)! If it were not for Runtime.exec() that allows you to execute native processes, I don't know what Java developers would do.

But then Runtime.exec() brings in a bigger problem. Your Java program no longer remains portable!! And hey, isn't portability a major reason why people use Java in the first place? How can you execute dir from your Java code on a Windows platform when you know that ls is the corresponding command for the Linux platform? Even if you do detect the current OS and make calls accordingly, will you take care of all the operating systems on which Java can run? That's hardly a solution.

Also, you'll need to ship these external native dependencies along with your software. If you want your code to be run on different platforms, you'll need to find a way to ship different versions of the dependencies for the different OS's.

Something to think about, huh?

I found an interesting article about the pitfalls of Runtime.exec(). Check it out.

Oh, and a "gotcha" that I keep coming back to: in Windows, for running OS commands which aren't in the form of executable files (like date or dir), you wanna execute "cmd /C <your command>" or preferably the array version of the same, not "date" or "dir" directly.

Tags: technology:java Last modified 06:58 Thu, 6 Mar 2008 by AmanKing. Accessed 227 times Children What Links Here share Share Except where expressly noted, this work is licensed under a Creative Commons License.