无法运行程序“...”错误=2,没有那个文件或目录(java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36901407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:46:45  来源:igfitidea点击:

Cannot run program "..." error=2, No such file or directory (java)

javamacosssh

提问by Spud

I am trying to make a java program that will set up a SSH connection for me on my macbook. It prompts me for the username, and then the IP address, then it is supposed to do "ssh username@ip".

我正在尝试制作一个 java 程序,它将在我的 macbook 上为我设置 SSH 连接。它提示我输入用户名,然后是 IP 地址,然后它应该执行“ssh username@ip”。

Below is my code:

下面是我的代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class SSH {
    public static void main(String[] args) throws Exception {
    boolean rep = true;
    String username = (null);
    String IPAdress = (null);
    while (rep) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Username:  ");
        username = scanner.next();
        System.out.print("\nIP Adress:  ");
        IPAdress = scanner.next();
        System.out.println("\n\nIs this correct?\nUsername:  " + username + "\nIP Adress:  " + IPAdress + "\nY/N");
        char responce = scanner.next().charAt(0);

        if (responce == 'Y' || responce == 'y') {
            rep = false;
            scanner.close();
        } else if (responce == 'N' || responce == 'n') {

        } else {
            Error displayErrorMessage = new Error();
            displayErrorMessage.displayError();
        }
    }
    String SSHStartup = username + "@" + IPAdress;
    System.out.println("Running command:  ssh " + SSHStartup);
    String[] command = { "/bin/bash, -c , ssh " + SSHStartup };
    Process p = Runtime.getRuntime().exec(command);
    p.waitFor();
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = "";
    StringBuffer output = new StringBuffer();
    while ((line = reader.readLine()) != null) {
        output.append(line + "\n");
    }
}
}

I know, its messy, and now well indented, but instead of executing the command, it gives me this:

我知道,它很乱,现在缩进得很好,但它没有执行命令,而是给了我这个:

Exception in thread "main" java.io.IOException: Cannot run program "/bin/bash, -c , ssh root@(ip here)": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:485)
at SSH.main(SSH.java:32)
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:248)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 3 more

For the purpose of this post, I have removed the IP address, but when I compile and run it, I try the actual one, and it gives me the same error.

为了这篇文章的目的,我已经删除了 IP 地址,但是当我编译和运行它时,我尝试了实际的,它给了我同样的错误。

Help?

帮助?

采纳答案by Kenster

String[] command = { "/bin/bash, -c , ssh " + SSHStartup };
Process p = Runtime.getRuntime().exec(command);

Your commandarray contains a single value, namely the string "/bin/bash, -c , ssh ...". Java is trying and failing to execute a file with that name.

您的command数组包含一个值,即字符串“/bin/bash, -c , ssh ...”。Java 尝试执行具有该名称的文件但失败。

You probably intended to construct a commandcontaining the command and its arguments as a sequence of strings, instead of a single string:

您可能打算构造一个command包含命令及其参数的字符串序列,而不是单个字符串:

String[] command = { "/bin/bash", "-c", "ssh " + SSHStartup };