I’ve inherited a Java web-services code-base (BEA/Oracle Weblogic) and need to start/launch an external background application from a web-service.
I’ve already tried:
ProcessBuilder pb = new ProcessBuilder(arg); pb.start();
as well as:
Runtime.exec(cmdString);
But am experiencing strange behaviors when launching applications in this manner (i.e. the launched application stops working even though the process is still active. — The application works fine when manually run from a normal command line).
Is there a better way to launch an external processes?
EDIT: ———————-
I have some additional information that may help shed some light on the problem.
- The process we are trying to start will require hours to complete so waiting for completion (using
waitfor()) in the webservice will not be an ideal scenario. - Yes, the process we are trying to start from the webservice was created by a fellow team member [cue: your eyes roll… now]
I have had success when I use process builder to start a bash script, where the external application is launched as a background process (using ‘&’).
#!/bin/bash java -jar myApp.jar &
This obviously creates an orphaned process but at least the application does continue to execute.
Simply put: if the launched application writes to SDTOUT/STDIN and you don’t flush them frequently (see Process.getErrorStream/Process.getInputStream) then the process will block when the buffer is full (that is really small, 4KB or less).
I recommend you to invoke ProcessBuilder.redirectErrorStream() before starting the process. Then, after that, create a thread with the run() method along the lines of: