1

Why does

ProcessBuilder pb = new ProcessBuilder("cmd","/C","dir"); 

work but

ProcessBuilder pb = new ProcessBuilder("cmd","dir"); 

does not.
I mean in the latter case the cmd starts but the listing of the directory does not happen.Why is this?

1
  • What does not work? Is an error thrown? Which? Commented Mar 8, 2013 at 9:38

2 Answers 2

5

It is the normal behaviour of cmd.exe - the same happens on the command line:

C:\>cmd dir Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. 
C:\>cmd /c dir Volume in drive C is System Volume Serial Number is ABCD-EF10 ... 

With the first call, you are creating a new (interactive) command interpreter process, cmd.exe. With the second call, you are creating a new command interpreter process and tell it to execute the given command and then exit:

/C Carries out the command specified by string and then terminates 
Sign up to request clarification or add additional context in comments.

Comments

3

Because cmd.exe works like that. Try this in a command window:

cmd dir 

and

cmd /C dir 

Also have a look at help cmd for an explanation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.