简体   繁体   English

如何正确使用ProcessBuilder

[英]How to use ProcessBuilder properly

I am trying to figure out how to use ProcessBuilder . 我试图弄清楚如何使用ProcessBuilder This trivial dir does not even work. 这个琐碎的dir甚至都不起作用。 What am I doing wrong? 我究竟做错了什么?

Process pb = new ProcessBuilder("cmd","dir C:\\").start();  
InputStream in = pb.getInputStream();  
BufferedReader br = new BufferedReader(new InputStreamReader(in));  
String line;  
while ((line = br.readLine()) != null) {  
    System.out.println(line);  
}  

The output is only: 输出仅为:

Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. Microsoft Windows XP [版本5.1.2600](C)版权所有1985-2001 Microsoft Corp.

Try 尝试

Process p = new ProcessBuilder("cmd", "/C", "dir")

Dir is a command of the shell. Dir是shell的命令。 /C tells the shell to interpret the parameters. / C告诉shell解释参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM