Why can't I run a command command from my java program?

I am 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 have tried many 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 it does.

Commands like echo will 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.

+3


source to share


1 answer


Because echo,

start

, rem

etc. are not executable programs. These are shell commands. Only the shell takes care of them. You have to start them with

cmd /c echo
cmd /c start chrome

      



and etc.

+6


source







All Articles