Principle: Java Runtime.getruntime (). EXEC (CommandText) can invoke the Execute CMD command.
cmd/c dir closes the command window after executing the dir command.
cmd/k dir does not close the command window after executing the dir command.
cmd/c start dir will open a new window and execute the dir instruction, the original window will be closed.
cmd/k start dir will open a new window and execute the dir instruction, the original window will not be closed.
Note: When start is added, a new window opens and you can use CMD/? To view help information.
public static void Runcmd (String path) throws Exception
{
Process p = runtime.getruntime (). EXEC ("cmd/c cmd.exe/c" + path+ "exit");
BufferedReader br = new BufferedReader (New InputStreamReader (P.getinputstream ()));
String readLine = Br.readline ();
while (readLine! = null) {
ReadLine = Br.readline ();
System.out.println (ReadLine);
}
if (br!=null) {
Br.close ();
}
P.destroy ();
P=null;
}
public static void Runcmdshow (String path) throws Exception
{
Process p = runtime.getruntime (). EXEC ("cmd/c start cmd.exe/c" + path+ "exit");
BufferedReader br = new BufferedReader (New InputStreamReader (P.getinputstream ()));
String readLine = Br.readline ();
while (readLine! = null) {
ReadLine = Br.readline ();
System.out.println (ReadLine);
}
if (br!=null) {
Br.close ();
}
P.destroy ();
P=null;
}
public static void Main (string[] args) {
String Path = "D:\\iimob\\tomcat2\\bin\\startup.bat";
System.out.println (New Date ());
try {
Runcmdshow (path);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println (New Date ());
}
Java Call cmd.exe command