简体   繁体   中英

How to use ProcessBuilder properly

I am trying to figure out how to use ProcessBuilder . This trivial dir does not even work. 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.

Try

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

Dir is a command of the shell. /C tells the shell to interpret the parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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