When writing Java programs, sometimes we need to invoke other programs or scripts such as Exe,shell. There are two methods available in Java to start other programs:
(1) using the runtime's Exec () method
(2) using the Processbuilder start () method
Runtime and Processbulider provide different ways to start a program,Set startup parameters, environment variables, and working directory. ButBoth of these methods will return aProcess object that manages operating system processes.
use Runtime.getruntime (). Ethe Xec () method can run an external program in a Java program.
1. EXEC (String command)
2. EXEC (String command, String envp[], File dir)
3. Exec (string cmd, string envp[])
4. EXEC (String cmdarray[])
5. EXEC (string cmdarray[], string envp[])
6. EXEC (string cmdarray[], string envp[], File dir)
A generic application can use the first version directly, using the later version when there is an environment variable passed. Versions 2 and 6 can pass a directory that identifies the current directory, because some programs use relative directories, so use this version.
cmd.exe/c start <FileName>
The call is also used when using DOS commands, such as Dir. If you want to interact with the calling program, you will use the return object process for the method, bythe getInputStream (), Getoutputstream (), and Geterrorstream () methods of the process can get the input and output stream, and then the output information of the program to the console can be obtained through InputStream,through the OutputStream can give the program input instructions, so as to achieve the program Exchange function.
When writing an application in Java, it is sometimes necessary to invoke another out-of-the-box executable or System command in the program, which can be implemented by combining the runtime classes and the process classes provided by Java. The following is a more typical program pattern:
Process process = Runtime.getruntime (). EXEC (". \\p.exe");
Process.waitfor ();
In the above program, the first line of ". \\p.exe" is the name of the program to execute,runtime.getruntime ()returnruntime object for the current application, the object'sexec ()method indicatesJava Virtual Machine creates a child process to execute the specified executable programAnd returns an instance of the process object corresponding to the child process. Process allows you to control the execution of the child process or obtain information about the child process. The purpose of the second statementwait for the child process to complete and then execute down.
However, on the Windows platform, if handled improperly, you sometimes do not get the expected results. The following is the author in the actual programming summary of several things to note:
1. Internal command to execute DOS
if you want to execute a DOS internal command, there are two methods. One way is to include the command interpreter in the parameters of the exec (). For example, to execute the dir command, on NT, can be written as exec ("cmd.exe/c dir"), under Windows95/98, can be written as "command.exe/c dir", where the parameter "/C" means that the command executes after the shutdown DOS immediately close the window. Alternatively, place internal commands in a batch command My_dir.bat file and write exec ("My_dir.bat") in the Java program. If only the exec ("dir") is written, the Java Virtual machine will report a run-time error. The first method is to ensure the portability of the program, you need to read the running operating system platform in the program to invoke a different command interpreter. The latter method does not need to do more processing.
2. Open a non-executable file
There are two ways to open a non-executable file, but there is an associated application for that file. To open a Word document A.doc file, for example, there are two ways to do this in Java:
EXEC ("start. \\a.doc");
EXEC ("Files\\Microsoft Office\\office\\winword.exe. \\a.doc");
Obviously, the former method is more simple and convenient.
3. Execute a DOS executable program with standard output
On the Windows platform,The DOS window running the invoked program does not automatically close after the program has finished executing, causing the Java application to block in the WAITFOR () statement. One possible cause of the phenomenon is thatthe standard output of the executable program is much larger, and the standard output buffer of the running window is not large enough. The solution is to use the method provided by the process class in Java to let the Java Virtual machine intercept the standard output of the calling program's DOS run window and read out the contents of the window's standard output buffer before the waitfor () command. A typical procedure is as follows:
String s;
Process process = Runtime.getruntime (). EXEC ("cmd/c dir \\windows");
BufferedReader BufferedReader =NewBufferedReader (NewInputStreamReader (Process.getinputstream ());
while((S=bufferedreader.readline ())! =NULL)
System.out.println (s);
Process.waitfor ();
Running external programs in Java using the runtime and process classes