import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
* 执行cmd命令
* @author kk
*
*/
public class CMD {
public static void main(String[] args) {
try {
String cmd = "ipconfig";//cmd命令
String cmd = "exp kk/1234@orcl file=d:\\databaseBackup\\edu.dmp owner=(kk)";//cmd命令
ArrayList<String> commands = new ArrayList<String>();
// cmd.split(" ") 命令行按照空格分割,放入集合中
commands.addAll(Arrays.asList(cmd.split(" ")));
//commands.add(cmd);
ProcessBuilder pb = new ProcessBuilder(commands);
pb.redirectErrorStream(true);
Process p = pb.start();//启动进程
BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));
String line;
while ((line = is.readLine()) != null) {
if (line.toLowerCase().startsWith("warning")) {
System.err.println("\tWARNING: " + line);
} else if (line.toLowerCase().startsWith("error")) {
System.err.println("\tERROR: " + line);
} else if (line.toLowerCase().startsWith("fatal")) {
System.err.println("\tFATAL ERROR: " + line);
} else {
System.out.println("\t" + line);
}
}
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("结束");
}
}
java执行cmd命令
最新推荐文章于 2024-08-27 16:47:52 发布