Skip to main content

How to get a realtime output of a process when it call using Runtime.exec()

No replies
rijeshpp
Offline
Joined: 2007-12-03

Hi,

How to get a real-time output (STDOUT) of a process when it execute using Runtime.exec()? We have developed java web based application deployed on WAS running on AIX server which calls a script deploy.sh using Runtime.exec() in servlet. Through the GUI screen when clicking on submit it invoke the script on the AIX server where application running. Currently it gives the STDOUT of the script on the GUI at the end of the script execution. Since the script execution of deploy.sh takes a long time (3-5 minutes), and the output (script output) gives only after 3-5 minutes user cannot identify what’s going on during that time. So looking for a solution to get a real-time output on the application's GUI. Is there any other way to get this?

Attaching herewith the current logic we have used in program, And our ultimate aim is TO SHOW USER CONCURRENT STDOUT on the application GUI screen.

// consoleString bind to TextArea

private StringBuffer consoleString;

// action for submit button

public String doSubmitAction() {

try {
runCommand(getCommand());
} catch (IOException e) {
consoleString.append(e.getMessage());
} catch (InterruptedException e1) {
consoleString.append(e1.getMessage());
}
renderedFlag = false;
return "";
}

private String getCommand() {

StringBuffer buffer = new StringBuffer();
ConfigurationManager configManager;
try {
configManager = ConfigurationManager.getInstance();
Resource resourceSeverConf = configManager.getResource(SERVER_CONF);
buffer.append(resourceSeverConf.getParameter("classpath"));
buffer.append(resourceSeverConf.getParameter("command") + " ");
buffer.append(getCbOperation().getValue() + " ");
buffer.append(getCbProjectName().getValue() + " ");
buffer.append(getCbStage().getValue() + " ");
buffer.append(getCbLocation().getValue() + " ");
buffer.append(getIpTxtBaseLine().getValue() + " ");
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}

private void runCommand(String cmd) throws IOException, InterruptedException {
consoleString = new StringBuffer("");
// set up list to capture command output lines
// start command running
Process proc = Runtime.getRuntime().exec(cmd);
// get command's output stream and
// put a buffered reader input stream on it
InputStream istr = proc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(istr));
// read output lines from command
String str;
while ((str = br.readLine()) != null) {
consoleString.append(str + "\n");
}
// wait for command to terminate
proc.waitFor();
// check its exit value
// if (proc.exitValue() != 0)
// System.err.println(proc.exitValue() + "exit value was non-zero");
// close stream
br.close();
}