【发布时间】:2014-10-25 19:03:30
【问题描述】:
为什么会出现以下错误:
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 本身不是可执行程序。
标签: java linux command-line terminal