Unable to run program “…” error = 2, no such file or directory (Java)

I'm trying to create a java program that will set up an SSH connection for me on my MacBook It prompts me for a user name, then an IP address, and then it should be "SSH username @ IP"

Here 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, it's messy. Now indent, but not execute the command. It gives me this:

Exception in thread "main" java.io.IOException: Cannot run program "/bin/bash,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 article, I have deleted the IP address, but when I compile and run it, I try the actual one, which gives me the same error

help?

Solution

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

Your command array contains a single value, the string "/ bin / bash, SSH..." Java is trying and cannot execute a file with that name

You may want to construct a command containing the command and its parameters as a string sequence instead of a single string:

String[] command = { "/bin/bash","-c","ssh " + SSHStartup };
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>