我试图从我的java应用程序运行命令。我从用户那里获得输入并运行以下代码:Runtime.getRuntime().exec($userInput);
。我尝试过很多简单的命令,包括" echo," "启动chrome,"和" rem。"似乎唯一有效的命令是" cmd.exe,"我甚至不知道那是什么。
" echo"等命令生成此错误:
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
随时向我询问更多详情。
答案 0 :(得分:6)
因为echo,
start
,rem
等不是可执行程序。它们是shell命令。只有命令shell才能完成它们。你必须用
cmd /c echo
cmd /c start chrome
等