4

I'm trying to run a command from my java application. I get input from the user and run this line of code: Runtime.getRuntime().exec($userInput);. I've tried lots of simple commands, including "echo," "start chrome," and "rem." It seems that the only command that works is "cmd.exe," and I have no idea what that even does.

Commands such as "echo" generate this error:

java.io.IOException: Cannot run program "echo": CreateProcess error=2, The system cannot find the file specified
            at java.lang.ProcessBuilder.start(Unknown Source)
            at java.lang.Runtime.exec(Unknown Source)
            at java.lang.Runtime.exec(Unknown Source)
            at java.lang.Runtime.exec(Unknown Source)
            at me.Draconwolver.Main.runCmd(Main.java:119)
            at me.Draconwolver.Main.main(Main.java:34)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
            at java.lang.ProcessImpl.create(Native Method)
            at java.lang.ProcessImpl.<init>(Unknown Source)
            at java.lang.ProcessImpl.start(Unknown Source)
            ... 6 more

Feel free to ask me for more details.

Synchronous
  • 193
  • 1
  • 3
  • 9
  • 1
    "echo" is a command of the DOS shell, it isn't directly a part of Windows itself. Your Java needs to run "cmd", and have cmd execute "echo". You'll find more details here: http://www.java2s.com/Questions_And_Answers/Java-File/batch-File/execute.htm – FoggyDay Aug 24 '14 at 23:11

1 Answers1

6

Because echo, start, rem etc aren't executable programs. They are shell commands. Only the command shell undertstands them. You have to run them with

cmd /c echo
cmd /c start chrome

etc.

user207421
  • 305,947
  • 44
  • 307
  • 483