标签:
当我尝试在java中通过ProcessBuilder运行window的cmd命令时出现错误:
public static void main(String [] args) throws IOException {
ProcessBuilder builder = new ProcessBuilder();
Process process = builder.command("dir d:\\").start();
InputStream inputStream = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"gb2312"));
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
}
Process process = builder.command("cmd.exe /c dir d:\\").start();
public static void main(String [] args) throws IOException {
ProcessBuilder builder = new ProcessBuilder();
List<String> list = new ArrayList<>();
list.add("cmd.exe");
list.add("/c");
list.add("dir");
list.add("d:\\");
Process process = builder.command(list).start();
InputStream inputStream = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"gb2312"));
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
}
public static void main(String [] args) throws IOException {
ProcessBuilder builder = new ProcessBuilder();
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd.exe /c dir d:\\");
InputStream inputStream = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"gb2312"));
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
}
标签:
原文地址:http://www.cnblogs.com/xjl456852/p/5839745.html