Applications occasionally need to start non-Java programs on the
local platfrom. For example, your server
(e.g. see Chapters 14, 15, 24) that controls a remote device might
need to run a diagnostic program that is only available as C executable
.
The java.lang.Runtime
and java.lang.Process
classes are available for executing and communicating with external
programs. With an instance of the Runtime
class, a program can be executed. The Process
instance for the program allows for some control of the program
and for access to the standard input, output, and error output streams
for the program.
Here is code to run the MS-DOS
batch file getDir.bat
that includes the line
dir
*.java
to do a directory listing on a Windows machine. A slightly different
command is needed on a Unix or Linux system. There are platform
portable ways to get a directory listing purely with Java code -
see the java.io.File.list()
method - but this example serves to demonstrate how to call external
programs.
The program reads the output from this program and prints it out.
RunTimeApp
|
import
java.io.*;
/** Demonstrate how to run an external program. **/
public class RunTimeApp {
/** From the main run an external local program.
**/
public static void main (String arg[]) {
try {
Runtime rt = Runtime.getRuntime
(); // step 1
Process process = rt.exec
("getDir.bat"); // step 2
InputStreamReader reader
= //
step 3
new InputStreamReader
( process.getInputStream () );
BufferedReader buf_reader
=
new BufferedReader
( reader ); //
step 4.
String line;
while ((line = buf_reader.readLine
()) != null)
System.out.println (line);
}
catch (IOException e) {
System.out.println (e);
}
} // main
} // class RunTimeApp
|
First an instance of the Runtime
class is obtained with the factory method:
Runtime rt =
Runtime.getRuntime ();
With the Runtime instance the program is run with the exec() method:
Process process
= rt.exec ("doDir.bat");
This method returns an instance of Process
that represents the external program. This class provides access
to the standard in, out, and err streams with which the Java program
can communicate with the external program.
In steps 3 and 4, the Process
object is used to read the output from the external process. Another
useful method on Process
is the waitFor()
method which can be used to force the calling Java thread to wait
until the external process terminates.
Note that running external programs clearly involves the details
of the particular host platform and OS. For example, the exec()
method does not use a shell. If a shell is needed, then it can be
run directly (ref. McCluskey).
There are several overloaded versions of exec()method
that allow for passing arguments to the process and setting environment
variables.
Running an external program also obviously limits portability.
However, in cases where the external program is available on multiple
platforms, such as for Web browsers, the application could use the
system properties to determine
the platform and then use this information to select a platform
specific command.
References & Web Resources
|