aspose file tools*
The moose likes Java in General and the fly likes How Direct Runtime.getRuntime().exec output to Console window ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How Direct Runtime.getRuntime().exec output to Console window ??" Watch "How Direct Runtime.getRuntime().exec output to Console window ??" New topic
Author

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

Jim Otte
Greenhorn

Joined: Feb 20, 2002
Posts: 6
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

Joined: Feb 20, 2002
Posts: 6
Found to be:
String command = "cmd /c start E:\\batFiles\\MyBat.bat";
Process pc = Runtime.getRuntime().exec(command);
Sandeep Lakshmipathy
Ranch Hand

Joined: Mar 05, 2002
Posts: 31
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();
}


Sandeep Lakshmipathy
divya g nair
Greenhorn

Joined: Jan 12, 2009
Posts: 2
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

Joined: Oct 13, 2005
Posts: 38865
    
  23
divya g nair, please don't add your own questions to somebody else's thread. You already have discussion elsewhere.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Direct Runtime.getRuntime().exec output to Console window ??