Search code examples
javacmdprocessbuilder

ProcessBuilder and cmd


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?


Solution

  • 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