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?
Hussain Akhtar Wahid 'Ghouri'
6,6379 gold badges52 silver badges87 bronze badges
asked Mar 8, 2013 at 9:35
Jim
19.7k41 gold badges147 silver badges267 bronze badges
-
What does not work? Is an error thrown? Which?user1907906– user19079062013年03月08日 09:38:00 +00:00Commented Mar 8, 2013 at 9:38
2 Answers 2
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
Andreas Fester
36.8k9 gold badges101 silver badges125 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Axel
14.2k6 gold badges55 silver badges80 bronze badges
Comments
lang-java