This is an instance of calling the dos command using Java code. I won't say much about it here, just upload the code, the code is as follows:
The code copy is as follows:
import java.io.*;
/**
* Java calls Windows DOS command
* Implementation: Call Windows' ipconfig command and then output the output information to the console through the IO stream.
*/
public class RunWindowsCommand{
public static void main(String[] args) {
InputStream ins = null;
String[] cmd = new String[] { "cmd.exe", "/C", "ipconfig" }; // Command
try {
Process process = Runtime.getRuntime().exec(cmd);
ins = process.getInputStream(); // Get 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); // Output
}
int exitValue = process.waitFor();
System.out.println("Return value:" + exitValue);
process.getOutputStream().close(); // Don't forget to close it
} catch (Exception e) {
e.printStackTrace();
}
}
}