JAVA执行本地bat文件

时间:2022-09-30 12:36:06

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;


public class cmdTest {

public static void main(String args[]){
callCmd("C:/xxxx/test.bat");
}
public static void callCmd(String locationCmd){
try {
Process child = Runtime.getRuntime().exec("cmd.exe /C start "+locationCmd);
InputStream in = child.getInputStream();
int c;
while ((c = in.read()) != -1) {
}
in.close();
try {
child.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("done");
} catch (IOException e) {
e.printStackTrace();
}
}
}