1
爲什麼我收到以下錯誤:有運行Linux在Java命令麻煩
Exception in thread "main" java.io.IOException: Cannot run program "cd": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at com.terminal.Main.main(Main.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:187)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 6 more
這裏是我使用的代碼:
Runtime.getRuntime().exec("cd ~/");
Process pwd = Runtime.getRuntime().exec("pwd");
try (Scanner scanner = new Scanner(pwd.getInputStream())) {
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
}
還當我嘗試執行其他一些命令,作爲sudo
或./
,IOException
再次發生... 有什麼問題?任何想法傢伙?
謝謝:)
您需要一個解釋器(shell)來運行* cd *命令。 * cd *本身不是可執行程序。 – agad 2014-09-01 14:25:04