UPDATE: I found a crucial part to why this probably isn't working! I used System.setOut(out); where out is a special PrintStream to a JTextArea
This is the code, but the issue I'm having is that the information is only printed out once I end the process.
public Constructor() {
main();
}
private void main() {
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ProcessBuilder builder = new ProcessBuilder("java", textFieldMemory.getText(), "-jar", myJar);
Process process = builder.start();
InputStream inputStream = process.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 1);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
inputStream.close();
bufferedReader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
});
}
Current output:
Line 1
Line 2
Line 3
Line 4
Line 5
This is the correct output, but it is only being printed as one big block when I end the process.
Does anyone know what the issue is? If so can you help explain to me why this is happening, thank-you in advance.
BufferedReader
, try just reading the contents from theInputStream
directly to see if it makes a difference – CalanBufferedReader
buffer size to 1 with its two parameter constructor:new BufferedReader(new InputStreamReader(inputStream), 1)
– Sandbyte
array and read directly from theInputStream
. The main reason I do this is because not all processes send newline characters as part of there output which means sometimes, you don't get any output and the process seems to never die – Calanprocess.waitFor
? And are you reading the stream output on a separate thread? – Customaryprocess.waitFor
and I haven't created any additional threads. – OutandoutSystem.setOut(out);
where out is a specialPrintStream
to aJTextArea
– OutandoutSystem.out
to a Swing component without ensuring that the output is correctly re-synced with the EDT – CalanPrintStream
then you need to ensure that any time it outputs to the text area, it's done within the context of the EDT. Take a look at my updated answer – Calan