Reputation: 387
I have a problem. Need to redirect process.getErrorStream(), process.getInputStream() and process.getOutputStream() to JTextPane. Process is situated in class A, and JTextPane is situated in class B, so there isn't direct connection between them. For this purpose I've create interface. So, I can call method informListener(String message) which appends line to JTextPane. But I can't find any solution which can solve my problem. Are there any nice and easy solutions?
Thanks.
Upvotes: 0
Views: 96
Reputation: 5463
What you need is couple of threads that read the data from the input streams returned by get*Stream
methods and append into the text area.
Something like:
final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream());
new Thread(new Runnable() {
String line ;
while ((line = reader.readLine()) != null) {
interfaceObject.informListener(line);
}
}).start();
Just ensure that the appending to textPane
happens in EDT using SwingUtilities.invokeLater
.
The following program works. (I am on OS X):
package snippet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ProcessOutput {
public static void main(String[] args) throws IOException, InterruptedException {
final Process p = Runtime.getRuntime().exec("ls -lR");
final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
new Thread(new Runnable() {
@Override public void run() {
try {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
}
}
}).start();
int waitFor = p.waitFor();
System.out.println(waitFor + " is the return");
}
}
Check whether your command is being constructed properly. May be just print it out and see whether you are able to execute it from cmdline.
Upvotes: 1
Reputation: 387
It doesn't work. InformaListener calls another method. Here is it's code:
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();
...
logTextPane.setEditorKit(kit);
logTextPane.setDocument(doc);
public void onLogData(final String message) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
kit.insertHTML(doc, doc.getLength(), message, 0, 0, null);
} catch (BadLocationException ex) {
Logger.getLogger(SummaryPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SummaryPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
And here is code to getErrorStream:
final String exec = "cmd /C start " + batStrPath + "\\upgrade-build.bat";
final Process p = Runtime.getRuntime().exec(exec);
final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
new Thread(new Runnable() {
@Override
public void run() {
try {
String line ;
while ((line = reader.readLine()) != null) {
informListener(line);
}
} catch (IOException ex) {
Logger.getLogger(Installer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}).start();
p.waitFor();
Upvotes: 0