java.lang.ProcessBuilder

Posted in blog city on May 28th 2004 

In JDK1.5, a new class ProcessBuilder has been added to java.lang package. This class should be used to execute OS specific commands. Prior to JDK1.5, developers use Runtime.exec to execute any OS specific commands.

I looked at the source code of Runtime.exec in JDK1.5, and it internally uses ProcessBuilder. The javadoc also clearly states that ProcessBuilder is the preferred way of meeting OS specific needs. However, the exec method in Runtime class is not deprecated. In my view, ideally exec should have been deprecated.

Also, the naming convention of methods in ProcessBuilder is strange. For example, consider the following two methods -

public File directory()
and
public ProcessBuilder directory(File directory)

While the first method  returns the current working directory, the latter actually sets the current working directory! Same is the case with command, redirectErrorStream methods.

Not really sure why one cannot name these as getWorkingDirectory() and setWorkingDirectory() that is more conventional in the java world.

May be I am missing something here!

Leave a Reply