I am fairly new to java and now I want to use java to run SSH over windows command.
Here is the code i created,
Process pr1 = Runtime.getRuntime().exec("cmd /k" + "ssh root@host" + "&&" + "passwd" );
Process pr = Runtime.getRuntime().exec("ls");
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null)
System.out.println(line);
I was always given the error :
java.io.IOException: Cannot run program “ls”: CreateProcess error=2,
The system cannot find the file specified
Can anybody help me on this?
Apart from using
JSch(or any other Java SSH implementation), passing the Path via environment variables is likely not to work, since most SSH deamons only accept a small set of variables from the other side (mostly related to localization or terminal type).As the argument to ssh (or the “command”, if using JSch with an ChannelExec) is passed to the remote shell for execution, you could try to define the path in this command (if your default shell is something compatible to the POSIX sh):
PATH=path_needed_toRun_myProg /absPathToMyProg/myProgYour array for Runtime.exec thus would look like this:
If its not hard and strict rule to use Runtime.exec, then try Apache’s Exec library…
See this link:
http://commons.apache.org/exec/