import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
public class UseCmd {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(new BufferedInputStream(System.in));
String commandStr = reader.nextLine();
runCmd(commandStr);
reader.close();
}
public static void runCmd(String commandStr) {
try {
// Runtime.getRuntime().exec(commandStr);
InputStream in = Runtime.getRuntime().exec(commandStr).getInputStream();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line=br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("\""+commandStr+"\""+ "不是内部或外部命令,也不是可运行的程序或批处理文件。");
// e.printStackTrace();
}
}
}
//class MyException extends IOException {
// String message;
// MyException(String commandStr) {
// message = commandStr + "不是内部或外部命令,也不是可运行的程序或批处理文件。";
// }
// public String getMessage() {
// return message;
// }
//}
Java调用cmd并回显结果
最新推荐文章于 2021-08-26 08:56:02 发布