Jim
Jim

Reputation: 19552

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.

Upvotes: 1

Views: 3647

Answers (1)

O.C.
O.C.

Reputation: 6819

Try

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

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

Upvotes: 3

Related Questions