Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中运行Linux命令时遇到问题_Java_Linux_Command Line_Terminal - Fatal编程技术网

在java中运行Linux命令时遇到问题

在java中运行Linux命令时遇到问题,java,linux,command-line,terminal,Java,Linux,Command Line,Terminal,为什么会出现以下错误: 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.NativeMethodAcc

为什么会出现以下错误:

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
而且,当我尝试执行一些其他命令时,例如
sudo
/
IOException
再次出现。。。 有什么问题?有什么想法吗,伙计们


谢谢:)

正如agad提到的,cd不是一个程序,它是一个shell命令。要更改exec()调用的工作目录,请使用:

正如您可能已经注意到的,这段代码仍然返回一个错误,因为使用tilde作为对主目录的引用是shell的另一个好处。您可以将“~/”替换为主目录,或者在未知的情况下,可以使用以下代码获取目录:

try {
    String homedir = System.getProperty("user.home");
    File wd = new File(homedir);
    Process pwd = Runtime.getRuntime().exec("pwd", null, wd);
    Scanner scanner = new Scanner(pwd.getInputStream());
    while (scanner.hasNextLine()) {
        System.out.println(scanner.nextLine());
    }
}
catch (Exception e) {
    System.out.println(e.getMessage());
}
如果您打算像这样运行多个命令,我建议您按照上面的链接,尝试使用支持多个命令的其他方法之一。

您需要一个解释器(shell)来运行cd命令。cd本身不是一个可执行程序。
try {
    File wd = new File("~/");
    Process pwd = Runtime.getRuntime().exec("pwd", null, wd);
    Scanner scanner = new Scanner(pwd.getInputStream());
    while (scanner.hasNextLine()) {
        System.out.println(scanner.nextLine());
    }
}
catch (Exception e) {
    System.out.println(e.getMessage());
}
try {
    String homedir = System.getProperty("user.home");
    File wd = new File(homedir);
    Process pwd = Runtime.getRuntime().exec("pwd", null, wd);
    Scanner scanner = new Scanner(pwd.getInputStream());
    while (scanner.hasNextLine()) {
        System.out.println(scanner.nextLine());
    }
}
catch (Exception e) {
    System.out.println(e.getMessage());
}