Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Runtime.exec  RSS feed

 
Carlos Vassallo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to run a class in a remote machine by ssh. The reasons for what I need to use ssh, are out of question (security �?)(the boss told me that !). I need to capture the output of that remote class so I call a .bat(who encapsulate the ssh call) and redirect the output to a file. After that, I try to read the output file by determinate amount of time(I can`t use the Process.waitFor() method).
The .bat works fine, but when I try to put the call into an Runtime.exec I have a problem, the call doesn't work.
If this work in a cmd, calling a .bat:
miBat param1 param2 param3
and I'm calling it in a
Process miProcess=(Runtime.getRuntime()).exec("cmd /c miBat param1 param2 param3");
this will work ?
No...
Some suggestions ?
Thanks.
 
Carlos Vassallo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I post this question, I was a little confused. My code now is working but I'm more confused than yesterday ...
Someone can tell me why I need to getInputStream(miProcess) and construct a BufferedReader so I can readLine() it ?
Process miProcess = miRun.exec("c:\\miBat.bat miPackage.miClass " + args[0] + " " + args[1] + " " + args[2] + " " + args[3]);
BufferedReader readIt=new BufferedReader (new InputStreamReader(miProcess.getInputStream()));
String line="";
while((line=readIt.readLine()) != null)
System.out.println(line);
...this is working and causes that my process terminates as I was expected with the correct output and exit code.
Thanks for your (no) help, if all my problems were solved by simply posting it in javaRanch my life will be so easy (and so bored, too).
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's the way exec() and the Process class were designed. They didn't want to make any assumtions about where the output should go. Maybe you want to direct it to the console; maybe you want to copy it to a file; maybe you want your program to read it, parse it, make decisions based on it; who knows? (Hopefully, you do.) But in order to do anything at all with the process output, you need to getInputStream() (output of the Process is input as far as your Java code is concerned) and then somehow read it and do whatever you want with it. Attaching a BufferedReader is a common choice, though it's a tiny bit risky - if the output ever ends without a line break, your program may hang, waiting for a line break that never comes. (Maybe the process has just written "Do you wish to continue (y/n)?" with no line break, and is waiting for a response.) I prefer something like:

If your program needs to interact with the process more, e.g. read its output and respond using getOutputStream() to send more data to the process, well, that gets more complicated...
Basically, a Process is designed to be very flexible in terms of how your program chooses to interact with it. This given you a lot of control, but also forces you to write more code doing things that seem like maybe they should be obvious (like writing the output somewhere).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!