java.lang package is implicitly imported into every Java program. This package's pitfalls surface often, affecting most programmers.
This month, I'll discuss the traps lurking in the Runtime.exec() method.The class java.lang.Runtime features a static method called getRuntime(), which retrieves the current Java Runtime Environment. That is the only way to obtain a reference to the Runtime object. With that reference, you can run external programs by invoking the Runtime class's exec() method. Developers often call this method to launch a browser for displaying a help page in HTML.
There are four overloaded versions of the exec() command:
public Process exec(String command);public Process exec(String [] cmdArray);public Process exec(String command, String [] envp);public Process exec(String [] cmdArray, String [] envp);For each of these methods, a command -- and possibly a set of arguments -- is passed to an operating-system-specific function
call. This subsequently creates an operating-system-specific process (a running program) with a reference to a Process class returned to the Java VM. The Process class is an abstract class, because a specific subclass of Process exists for each operating system.
You can pass three possible input parameters into these methods:
Pass in the environment variables in the form name=value. If you use the version of exec() with a single string for both the program and its arguments, note that the string is parsed using white space as the delimiter
via the StringTokenizer class.
The first pitfall relating to Runtime.exec() is the IllegalThreadStateException. The prevalent first test of an API is to code its most obvious methods. For example, to execute a process that is external
to the Java VM, we use the exec() method. To see the value that the external process returns, we use the exitValue() method on the Process class. In our first example, we will attempt to execute the Java compiler (javac.exe):
Listing 4.1 BadExecJavac.java
import java.util.*;
import java.io.*;
public class BadExecJavac
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("javac");
int exitVal = proc.exitValue();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
A run of BadExecJavac produces:
E:\classes\com\javaworld\jpitfalls\article2>java BadExecJavac
java.lang.IllegalThreadStateException: process has not exited
at java.lang.Win32Process.exitValue(Native Method)
at BadExecJavac.main(BadExecJavac.java:13)
If an external process has not yet completed, the exitValue() method will throw an IllegalThreadStateException; that's why this program failed. While the documentation states this fact, why can't this method wait until it can give a
valid answer?
JConfig library or download it for evaluation
This artcle is very good. Thnaks for your helpBy Anonymous on February 9, 2010, 8:59 amThis artcle is very good. Thnaks for your help
Reply | Read entire comment
Nice article, it resolved my 'rar.exe' deaklock problemBy Anonymous on January 27, 2010, 6:50 amI write waitFor() BEFORE consuming StdOut/StdErr stream of the process, so the 'rar.exe LARGE_FILE' process is deadlock. Moved waitFor() AFTER consuming StdOut/StdErr...
Reply | Read entire comment
hai i need to execte the command "javac" by using this how can iBy Anonymous on January 17, 2010, 1:50 pmhai i need to execte the command "javac" by using this how can i do this
Reply | Read entire comment
Thank you for the great article! But something is missing, how dBy Anonymous on December 20, 2009, 5:15 amThank you for the great article! But something is missing, how do I properly send commands to an already runnnig process? Of course I have tried the process.getOutputStream(),...
Reply | Read entire comment
Application exit value 128By Anonymous on December 18, 2009, 8:43 amHello, i use the Runtime exec with the Class "StreamGobbler" to execute one of my application (exe) and the exit value of the watFor is 128. There is no such return...
Reply | Read entire comment
View all comments