Blog Source: Runtime.getruntime (). exec
When writing an application in Java, it is sometimes necessary to invoke another out-of-the-box executable or System command in a program, which can be implemented by combining the runtime class and the process class provided by Java. Here 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 () returns the Runtime object for the current application, and the Exec () method of that object instructs the Java virtual machine to create a child process to execute the specified executable program. and returns an instance of the process object corresponding to the child process. Process enables you to control the execution of the subprocess or to obtain information about the child process. The purpose of the second statement waits for the child process to complete and then down.
But on the Windows platform, if mishandled, sometimes the expected results are not available. The following is a summary of the author's practical programming in several cases to be noted:
1. Execute DOS Internal command
If you want to execute a DOS internal command, there are two ways. One approach is to include the command interpreter in the parameters of exec (). For example, executing the dir command, on NT, can be written as exec ("cmd.exe/c dir"), under Windows 95/98, written "command.exe/c dir", where parameter "/C" indicates that the command is executed to close the DOS window immediately Mouth. Alternatively, the internal commands are placed in a batch of command My_dir.bat files, written as exec ("My_dir.bat") in the Java program. If you write only exec ("dir"), the Java Virtual machine will report a run-time error. The previous approach 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 approach does not require more processing.
2. Open a file that is not executable
There are two ways to open a file that is not executable, but the file has an associated application. To open a Word document A.doc file For example, there are two ways to do this in Java:
EXEC ("start.//a.doc");
EXEC ("C://program 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 Windows platforms, a DOS window running the invoked program often does not automatically shut down after the program has been executed, causing the Java application to block in waitfor (). One possible cause of this behavior is that the executable program has more standard output than the standard output buffer of the running window is not large enough. The solution is to use the method provided by the Java-provided process class to let the Java Virtual machine intercept the standard output of the DOS run window of the invoked program and read the contents of the window's standard output buffer before the waitfor () command. A typical procedure is as follows:
...
String Ls_1;
Process process = Runtime.getruntime (). EXEC ("cmd/c dir//windows");
BufferedReader BufferedReader = new BufferedReader (/
New InputStreamReader (Process.getinputstream ());
while ((Ls_1=bufferedreader.readline ())!= null)
System.out.println (Ls_1);
Process.waitfor ();
...