Java method that invokes the DOS command for Windows
- 2020-04-01 03:43:04
- OfStack
This is an instance of using Java code to call DOS command, here I won't say much, directly on the code, the code is as follows:
import java.io.*;
/**
* Java call windows the DOS The command
* To call Windows the ipconfig Command and then pass the output information through IO Stream output to the console.
*/
public class RunWindowsCommand{
public static void main(String[] args) {
InputStream ins = null;
String[] cmd = new String[] { "cmd.exe", "/C", "ipconfig" }; //The command < br / >
try {
Process process = Runtime.getRuntime().exec(cmd);
ins = process.getInputStream(); //Gets the information after executing the CMD command
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line); //O < br / >
}
int exitValue = process.waitFor();
System.out.println(" The return value: " + exitValue);
process.getOutputStream().close(); //Don't forget to close
} catch (Exception e) {
e.printStackTrace();
}
}
}