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?

asked Mar 8, 2013 at 9:35
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
answered Mar 8, 2013 at 9:39
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.

answered Mar 8, 2013 at 9:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.