• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.exec() and ssh help reqd

 
Jagat Bandhu
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done something like the following in my code:


[code]
________________________________________________________
somefunc() returning String[]
{
String[] callargs = {"ssh", "-2", "-v", "-l", "alpha",
"122.1.18.169","ls"};


Runtime r = Runtime.getRuntime();


Process p = r.exec(nargs);


BufferedReader is = new BufferedReader(new
InputStreamReader(p.getInputSt�ream()));
BufferedReader es = new BufferedReader(new
InputStreamReader(p.getErrorSt�ream()));
OutputStream stdOut = p.getOutputStream();


ArrayList res = new ArrayList();
String line;


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


while ((line = is.readLine()) != null)
{
System.out.println(line);
res.add(line);


}


String str;
while ((str = es.readLine()) != null)
{
System.out.println(str);


}


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


}

________________________________________________________
[code]

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.


Thanks!

[ August 08, 2005: Message edited by: Jagat Bandhu ]
[ August 08, 2005: Message edited by: Jagat Bandhu ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.
 
Jagat Bandhu
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) {
System.out.println(str);
}
______________________________________________

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?

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic