270

I can understand cmd but not cmd /c. I was trying to invoke a java program from the current for which I use Runtime.getRuntime().exec("cmd /C java helloworld"); There arises my doubt.

Chris
  • 1,378
  • 17
  • 29
user62835
  • 3,019
  • 3
  • 18
  • 8
  • 7
    To Moderators, is there a provision to move this to appropriate portal/subsidiary of stack overflow. That way this item will not be closed and users will be able to contribute more? I guess this should go to something similar to super user? – digitally_inspired Jul 25 '18 at 16:14

3 Answers3

308

The part you should be interested in is the /? part, which should solve most other questions you have with the tool.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>cmd /?
Starts a new instance of the Windows XP command interpreter

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
    [[/S] [/C | /K] string]

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains
/S      Modifies the treatment of string after /C or /K (see below)
/Q      Turns echo off
/D      Disable execution of AutoRun commands from registry (see below)
/A      Causes the output of internal commands to a pipe or file to be ANSI
/U      Causes the output of internal commands to a pipe or file to be
        Unicode
/T:fg   Sets the foreground/background colors (see COLOR /? for more info)
/E:ON   Enable command extensions (see below)
/E:OFF  Disable command extensions (see below)
/F:ON   Enable file and directory name completion characters (see below)
/F:OFF  Disable file and directory name completion characters (see below)
/V:ON   Enable delayed environment variable expansion using ! as the
        delimiter. For example, /V:ON would allow !var! to expand the
        variable var at execution time.  The var syntax expands variables
        at input time, which is quite a different thing when inside of a FOR
        loop.
/V:OFF  Disable delayed environment expansion.
Blorgbeard
  • 97,316
  • 47
  • 222
  • 267
guyumu
  • 3,217
  • 2
  • 18
  • 18
  • 5
    there's also a command to "pause" the cmd window after output: "cmd /c file_name & PAUSE" it is bit different from cmd /k – GorvGoyl Sep 05 '15 at 19:34
  • what if a given command is kind of a server that starts it's own loop? should we use /C or /K? – redpix_ Mar 27 '17 at 20:21
  • While executing command in C# via 'Process' is using /C mandatory in Windows 7 or Windows Server 2008 R2 operating system? – Dinesh Kumar P Jun 12 '17 at 11:14
  • I came here becasue its not behaving as described. Im runnig from git bash and cmd /c "dir" or any command will just leave me in cmd prompt without executing the ocmmand. – Brad Oct 23 '20 at 22:44
  • cmd /? | more ...to page the command switches – Prisoner ZERO Apr 20 '21 at 18:01
129

/C Carries out the command specified by the string and then terminates.

You can get all the cmd command line switches by typing cmd /?.

T30
  • 10,276
  • 6
  • 47
  • 57
Simon Temlett
  • 2,347
  • 2
  • 17
  • 17
  • 11
    But why would one want to? Why not just call the command directly, like "java helloworld" – Mike Sadler Apr 11 '19 at 11:04
  • 2
    because you might not be in CMD but you just wan to use cmd to execute a command for you. eg I am running a bash script and want it to execute a command that cmd can do but bash can not. – Brad Oct 23 '20 at 22:58
28
CMD.exe

Start a new CMD shell

Syntax
      CMD [charset] [options] [My_Command] 

Options       

**/C     Carries out My_Command and then
terminates**

From the help.

phuclv
  • 32,499
  • 12
  • 130
  • 417
Andrea Ambu
  • 36,488
  • 14
  • 52
  • 76