java.io.IOException: CreateProcess: " ssh2 -l username serverbox" error=2

Discussion in 'Java' started by csduvvuri@gmail.com, Jun 28, 2006.

  1. Guest

    i have written a java program to connect to a Server box using SSH2 but
    when i am executing the code its showing some eception. first I am
    trying to invoke command prompt but the console is disappearing as soon
    it is invoked.

    the exception is

    java.io.IOException: CreateProcess: "ssh2 -l username serverbox "
    error=2
    at java.lang.Win32Process.create(Native Method)
    at java.lang.Win32Process.<init>(Win32Process.java:87)
    at java.lang.Runtime.execInternal(Native Method)
    at java.lang.Runtime.exec(Runtime.java:582)
    at java.lang.Runtime.exec(Runtime.java:505)
    at java.lang.Runtime.exec(Runtime.java:471)
    at Sam.doExec2(Sam.java:29)
    at Sam.main(Sam.java:35)
    Exception in thread "main"


    The code is




    import java.io.IOException;

    public class Sam{

    static void doExec1() throws IOException {

    // invoke a shell and give command to it

    Runtime runtime = Runtime.getRuntime();
    String[] args =
    new String[]{"cmd"};

    Process p = runtime.exec(args);
    System.out.println("The command prompt");
    }

    static void doExec2() throws IOException {

    // invoke a shell and give command to it
    System.out.println("the second method");
    Runtime runtime = Runtime.getRuntime();
    System.out.println("the second method1");
    String[] args =
    new String[]{"ssh2 -l username serverbox "};
    System.out.println("the second method2");

    Process p = runtime.exec(args);
    System.out.println("the second method3");
    }

    public static void main(String[] args) throws IOException {
    doExec1();
    doExec2();
    }
    };


    please anyone help me

    Thanks
    Chandoo
    , Jun 28, 2006
    #1
    1. Advertising

  2. Re: java.io.IOException: CreateProcess: " ssh2 -l username serverbox"error=2

    On 28-6-2006 8:59, wrote:
    > i have written a java program to connect to a Server box using SSH2 but
    > when i am executing the code its showing some eception. first I am
    > trying to invoke command prompt but the console is disappearing as soon
    > it is invoked.
    >
    > the exception is
    >
    > java.io.IOException: CreateProcess: "ssh2 -l username serverbox "
    > error=2
    > [...]
    > please anyone help me
    >
    > Thanks
    > Chandoo
    >

    In your program you are using the exec method that takes a String array
    as arguments. In this array the first element is the program to be
    executed, the remaining elements are the arguments for this program.
    Your array has only one element: it tries to start the program "ssh2 -l
    username serverbox.exe", which obviously didn't exist.

    Split the command and its arguments yourself [2], or let the
    Runtime.exec(String) do it four you [1].

    So, use [1]:
    String command = "ssh2 -l username serverbox";
    Process p = Runtime.getRuntime().exec(command);
    <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#exec(java.lang.String)>

    Or [2]:
    String[] cmdarray = {"ssh2", "-l", "username", "serverbox"};
    Process p = Runtime.getRuntime().exec(cmdarray);
    <http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#exec(java.lang.String[])>
    --
    Regards,

    Roland
    Roland de Ruiter, Jun 28, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kroum Grigorov
    Replies:
    1
    Views:
    700
    Christopher Benson-Manica
    Apr 26, 2004
  2. Elly Sokol

    CreateProcess

    Elly Sokol, Jun 17, 2004, in forum: C++
    Replies:
    13
    Views:
    4,412
    Frederic Banaszak
    Jun 18, 2004
  3. Baskar

    Createprocess

    Baskar, Jan 20, 2005, in forum: C++
    Replies:
    1
    Views:
    448
    Victor Bazarov
    Jan 20, 2005
  4. Achim Domma

    CreateProcess / redirect output

    Achim Domma, May 28, 2004, in forum: Python
    Replies:
    2
    Views:
    7,184
    Achim Domma
    May 28, 2004
  5. Sonal

    JSPC : createProcess error.

    Sonal, Oct 11, 2007, in forum: Java
    Replies:
    0
    Views:
    1,267
    Sonal
    Oct 11, 2007
Loading...

Share This Page