public final class PowerBuilderProcessRunner {
private PowerBuilderProcessRunner() {}
private static class StreamHandler implements Callable<Void> {
private BufferedReader reader;
private OutputStream outputStream;
private PrintWriter writer;
public StreamHandler(InputStream inputStream, OutputStream outputStream) {
this.reader = new BufferedReader(new InputStreamReader(inputStream));
this.outputStream = outputStream;
this.writer = new PrintWriter(this.outputStream);
}
@Override
public Void call() throws Exception {
String line;
while ((line = reader.readLine()) != null) {
writer.println("OUTPUT FROM POWERBUILDER: " + line);
writer.flush();
Thread.yield();
}
return null;
}
}
// Call this method to start powerbuilder
public static void spawnProcess(/* arguments go here */) throws Exception {
ProcessBuilder builder = new ProcessBuilder(
"powerbuilder.exe", // or similar
"arg1", // taken from arguments -> must be string
"arg2", // taken from arguments -> must be string
);
Process process = builder.start();
ExecutorService threadPool = Executors.newFixedThreadPool(2);
threadPool.submit(new StreamHandler(process.getInputStream(), System.out));
threadPool.submit(new StreamHandler(process.getErrorStream(), System.err));
process.waitFor();
}
}