无法运行程序“...” error = 2,没有这样的文件或目录(java) [英] Cannot run program "..." error=2, No such file or directory (java)

查看:507
本文介绍了无法运行程序“...” error = 2,没有这样的文件或目录(java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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".

下面是我的代码:

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

就本帖子而言,我有删除了IP地址,但是当我编译并运行它时,我尝试了实际的一个,一个d它给了我同样的错误。

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.

帮助?

推荐答案

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

您的命令数组包含单个值,即字符串/ bin / bash,-c,ssh ...。 Java正在尝试并且无法执行具有该名称的文件。

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

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

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

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

这篇关于无法运行程序“...” error = 2,没有这样的文件或目录(java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆