Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Direct Runtime.getRuntime().exec output to Console window ??

 
Jim Otte
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem: I want to execute .bat in separate console window with output to that console window.
Solution 1:
String command = "cmd /c E:\\batFiles\\MyBat.bat";
Process pc = Runtime.getRuntime().exec(command);

Does not show output at all
Solution 2:
String command = "cmd /c E:\\batFiles\\MyBat.bat";
Process pc = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new
InputStreamReader(pc.getInputStream()));
String response;
while ((response = in.readLine()) != null) {
System.out.println("-" + response);
}
Prints to std.out- not to new dos window-
Question- what needed to direct inputStream to new console window???
Thanks
 
Jim Otte
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found to be:
String command = "cmd /c start E:\\batFiles\\MyBat.bat";
Process pc = Runtime.getRuntime().exec(command);
 
Sandeep Lakshmipathy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A more generic approach would be some thing like this
//.........
Process p = Runtime.getRuntime().exec(cmd);
if(p.waitFor() != 0)
System.out.println(getOutAndErrStream(p));
//
private String getOutAndErrStream(Process p){

StringBuffer cmd_out = new StringBuffer("");
if(p != null){
BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
String buf = "";
try{
while((buf = is.readLine()) != null){
cmd_out.append(buf);
cmd_out.append (System.getProperty"line.separator"));
}
is.close();
is = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while((buf = is.readLine()) != null){
cmd_out.append(buf);
cmd_out.append("\n");
}
is.close();
}catch(Exception e){
e.printStackTrace();
}
}
return cmd_out.toString();
}
 
divya g nair
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread hijack.
Removed from this thread.
Thread mentioned by Campbell below can be found here
http://www.coderanch.com/t/425969/Java-General-intermediate/Help-for-getting-output-of
 
Campbell Ritchie
Sheriff
Pie
Posts: 48964
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
divya g nair, please don't add your own questions to somebody else's thread. You already have discussion elsewhere.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic