Reputation: 19562
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?
Upvotes: 1
Views: 3857
Reputation: 14159
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.
Upvotes: 3
Reputation: 36630
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
Upvotes: 5