package de.tutorials;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class LogProcessBuilder {
public static class LogFrame extends JFrame {
private static final String LINE_SEP = System
.getProperty("line.separator");
private JTextArea log;
public LogFrame(InputStream stream) {
super("log");
init();
readFromStream(stream);
}
private void init() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
log = new JTextArea();
JScrollPane scroll = new JScrollPane(log);
add(scroll);
setSize(400, 400);
}
private void readFromStream(InputStream stream) {
final BufferedReader r = new BufferedReader(new InputStreamReader(
stream));
try {
String line = null;
while ((line = r.readLine()) != null) {
log.append(line);
log.append(LINE_SEP);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
final File f = new File("batch.sh");
final ProcessBuilder b = new ProcessBuilder(f.getAbsolutePath());
b.directory(f.getParentFile());// Ausführungsverzeichnis setzen
b.redirectErrorStream(true);
final Process process = b.start();
JFrame logFrame = new LogFrame(process.getInputStream());
logFrame.setVisible(true);
int term = process.waitFor();
System.out.println("Process terminated successfully: " + (term == 0));
}
}