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, the dir command, on NT, can be written as exec ("cmd.exe/c dir") and, under Windows 95/98, written as "Command.exe/c dir", where parameter "/C" means to close the DOS immediately after the command is executed. 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 filesmicrosoft Officeofficewinword.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 ();
...
The above content is reproduced ~ below content for original!
Today, the automatic updating of the client program, a simple description, that is, from the server to download the update package, and then in the local decompression, and finally deleted ~ function is very simple.
But the problem is that using the Java Zip module to do file decompression is not as simple as it might seem, resources need to be released, a careless there is no way to delete the original ZIP file ~ Resource occupancy is really a big problem, but fortunately, the client program is to restart the update, everything has vanished ~ For the deletion of the zip file, I also rogue a bit ~ with Del hard to remove ~ here must pay attention!
Process process = Runtime.getruntime (). EXEC ("cmd/c del f:aaa.doc");
Such a call is no problem ~
Process process = Runtime.getruntime (). EXEC ("del f:aaa.doc");
It is impossible to write in this way.