Both processbuilder.start()and runtime.exe c can create a local (native) Process and return a Java Process instance representing the Process.
The Java. lang. process class can be used to control the process and obtain some information about the process.
(1) Call the system command to create the process and obtain the command output. Use runtime.exe c (String cmd ):
Public class ListNetStatus {
Public static String executeCommand (String cmd) throws IOException {
Process ps = runtime.getruntime(cmd.exe c (cmd );
Vertex vertex = new vertex (ps. getInputStream ());
StringBuilder result = new StringBuilder ();
While (iterator. hasNextLine ()){
Result. append (response. nextLine ());
Result. append (System. getProperty ("line. separator "));
}
Response. close ();
Return result. toString ();
}
// List the current network status of the server
Public static void main (String [] args) throws InterruptedException, IOException {
System. out. println (executeCommand ("netstat "));
}
}
In JDK, Runtime.exe c (String cmd) still calls the runtime.getruntime(cmd.exe c (String [] cmdarray, String [] envp, File dir) method,
Including exec (cmdarray, envp1_, runtime.getruntime(cmd.exe c (cmdarray.
Next, let's focus on studying runtime.getruntime(cmd.exe c (String [] cmdarray, String [] envp, File dir ),
It indicates that the given commands and parameters are executed in a specific environment and in a specific working directory.
1. String [] cmdarray is a String array parameter, indicating the command to be called and the parameter of this command
2. String [] envp indicates the environment variable. Its storage format is name = value or null. If envp is null, the created sub-process inherits the environment variable of its parent process.
3. File dir sub-process working directory. If it is null, the sub-process inherits the working directory of the parent process.
(2) Next we will demonstrate this method for this program,This program runs under JDK 6.0 to start a sub-process, rewrite its environment variables such as JAVA_HOME and Path, change them to the runtime environment of JDK, and set the working directory, then, call Java-version to display the current environment and the source file ProcessTest1.java.
First, the current running environment is as follows:
The program code is as follows:
Public class ProcessTest1 {
Public static String executeCommand (String [] cmd, String [] env, File dir)
Throws IOException, InterruptedException {
Process ps = runtime.getruntime(cmd.exe c (cmd, env, dir );
Repeated scanner1 = new second (ps. getInputStream ());
Repeated scanner2 = new round (ps. getErrorStream ());
StringBuilder result = new StringBuilder ();
While (scanner1.hasNextLine ()){
Result. append (scanner1.nextLine ());
Result. append (System. getProperty ("line. separator "));
}
While (scanner2.hasNextLine ()){
Result. append (scanner2.nextLine ());
Result. append (System. getProperty ("line. separator "));
}
Scanner1.close ();
Scanner2.close ();
Return result. toString ();
}
Public static void main (String [] args) throws InterruptedException,
IOException {
// Set the working directory
String [] env = {"JAVA_HOME = C:/Program Files/Java/jdk1.5.0 _ 06 ",
"PATH = C:/Program Files/Java/jdk1.5.0 _ 06/bin "};
// Set Environment Variables
File dir = new File (
"D:/eclipse2/workspace/RegExpProject/com/test/process ");
// Display the current Java Runtime Environment Information
System. out. println (executeCommand (new String [] {"cmd", "/c", "java ",
"-Version"}, env, dir ));
// Call the current version of Java compiler to compile the source code
System. out. println (executeCommand (new String [] {"cmd", "/c", "javac ",
"ProcessTest1.java"}, env, dir ));
}
}
The program running result is as follows:
Then, view the ProcessTest1.class file generated at this time and use the command in jdk:Javap-verbose ProcessTest1For its disassembly, we will see the major version: 49 of the class at this time. If we have some knowledge about the JDK version, we will know that the class is compiled by Java 5 at this time, rather than Java6.
For more information, see the following table (from KenWu's blog ):
(3) The ProcessBuilder class is a new class of J2SE 1.5's new feature in java.lang. it can also be used to create sub-paths and is more convenient to use than runtime.exe c.
The following code demonstrates the function of displaying the Java Runtime Environment in ProcessBuilder (2:
Public class ProcessTest2 {
Public static void main (String [] args) throws InterruptedException,
IOException {
ProcessBuilder pb = new ProcessBuilder ("cmd", "/c", "java", "-version ");
// If the value is set to true, the standard error will be merged with the standard output. This makes it easier to associate error messages with the corresponding output.
// In this case, the merged data can be read from the stream returned by Process. getInputStream ().
Pb. redirectErrorStream (true );
Pb. directory (new File (
"D:/eclipse2/workspace/RegExpProject/com/test/process "));
String path = pb. environment (). get ("Path ");
Pb. environment (). put ("Path", "C:/Program Files/Java/jdk1.5.0 _ 06/bin ");
Pb. environment (). put ("JAVA_HOME", "C:/Program Files/Java/jdk1.5.0 _ 06 ");
Process ps = pb. start ();
Vertex vertex = new vertex (ps. getInputStream ());
StringBuilder result = new StringBuilder ();
While (iterator. hasNextLine ()){
Result. append (response. nextLine ());
Result. append (System. getProperty ("line. separator "));
}
Response. close ();
System. out. println (result. toString ());
}
}
PS:
Download the source code in this article: processtest.zip