0

below is the piece of code I am running. whether I use an array pf string (cmd) or it is a single string I get an exception (see further down) while there is a passwordless login to the target Linux system.

private static int bringHostFile() {
    try {            
        String[] cmd ={"ssh" , "root@im6-64s" , "/root/bring_hosts"};
   Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader stdInput = new BufferedReader(new 
             InputStreamReader(p.getInputStream()));

        BufferedReader stdError = new BufferedReader(new 
             InputStreamReader(p.getErrorStream()));

        String s = null;
        // read the output from the command
        if ((s = stdInput.readLine()) != null) {
            System.out.println(s);     
         }

        // read any errors from the attempted command
        while ((s = stdError.readLine()) != null) {
               System.out.println(s);
        }            

    }
    catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }   

    return 0;  
}

The exception :

java.io.IOException: Cannot run program "ssh": CreateProcess error=2, The system cannot find the file specified.
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:471)
    at java.lang.Runtime.exec(Runtime.java:604)
    at java.lang.Runtime.exec(Runtime.java:442)
    at java.lang.Runtime.exec(Runtime.java:339)
    at JavaRunCommand.CommandGetCurrentCPUSize(JavaRunCommand.java:140)
    at EC.<init>(EC.java:29)

Any idea what is the cause ?

3
  • 3
    "The system cannot find the file specified": is ssh in your program's %PATH%? Commented Mar 30, 2012 at 13:28
  • Or, you do not have a ssh client installed on your Windows machine. Commented Mar 30, 2012 at 13:29
  • You can specify the full path of the ssh if its not in your path. Commented Mar 30, 2012 at 16:26

2 Answers 2

5

The best way to do this in a portable manner would be to use a pure Java SSH implementation

Jsch is one of them, and a good one

It'll avoid you to cope with classpath/environnement problems while letting you send any command you want to your remote box

Sign up to request clarification or add additional context in comments.

Comments

0

make sure you have ssh client, and it's in %PATH% (try open 'cmd' and exec ssh from there). you can also use putty or cygwin for that, or the most recommended way- use java library: SSH library for Java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.