• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found to be:
String command = "cmd /c start E:\\batFiles\\MyBat.bat";
Process pc = Runtime.getRuntime().exec(command);
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread hijack.
Removed from this thread.
Thread mentioned by Campbell below can be found here
https://coderanch.com/t/425969/Java-General-intermediate/Help-for-getting-output-of
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic