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

C

csduvvuri

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
 
R

Roland de Ruiter

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[])>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top