Meaningless Drivel is fun!
The moose likes Java in General and the fly likes Runtime.exec() and ssh help reqd Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.exec() and ssh help reqd" Watch "Runtime.exec() and ssh help reqd" New topic

Runtime.exec() and ssh help reqd

Jagat Bandhu

Joined: Aug 08, 2005
Posts: 9
I have done something like the following in my code:

somefunc() returning String[]
String[] callargs = {"ssh", "-2", "-v", "-l", "alpha",

Runtime r = Runtime.getRuntime();

Process p = r.exec(nargs);

BufferedReader is = new BufferedReader(new
BufferedReader es = new BufferedReader(new
OutputStream stdOut = p.getOutputStream();

ArrayList res = new ArrayList();
String line;

System.out.println("So far OK!");

while ((line = is.readLine()) != null)


String str;
while ((str = es.readLine()) != null)


return (String[])res.toArray(new String [res.size()] );



The intention is to get all the file/folder listings from the remote
machine into a string array. I have already implemented a rsa key
policy, so password checking is not required.

However the problem is that after i run the code, it prints upto the
line which prints System.out.println("So far OK!"). But after that
nothing is happening. It just hangs!

Could anyone guide me to the problem?
Appreciate your help on this.


[ August 08, 2005: Message edited by: Jagat Bandhu ]
[ August 08, 2005: Message edited by: Jagat Bandhu ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

This is just a guess, but I'm fairly sure of it. The child process' output goes into a buffer, one for stdout, and one for stderr. It's up to the parent process to drain those buffers, or otherwise the child gets a SIGPAUSE and has to wait until there's room in the buffer again. I think that's what's happening here. The -v switch causes ssh to emit a lot of output to stderr, but your process isn't reading stderr, it's reading stdout only. The stderr buffer fills up and the child pauses before you ever see the output you expect.

The easiest thing for you to do here would be to drop the "-v" argument. Otherwise, you're going to need to use a separate thread to drain stderr.
Note that this isn't a Java thing, but a UNIX thing.

[Jess in Action][AskingGoodQuestions]
Jagat Bandhu

Joined: Aug 08, 2005
Posts: 9
Thanks for your answer.
However, I'm indeed reading the error also:
Pls note the following lines in the code-
while ((str = es.readLine()) != null) {

This should solve any buffer problem you hae mentioned.

Also, even if I eliminate the -v option, even then the program hangs.

Any other suggestions?

I agree. Here's the link:
subject: Runtime.exec() and ssh help reqd
jQuery in Action, 3rd edition