So I'm creating a gui for an exe in Java. Bit cumbersome but I'm trying. Due to this program being a console program, I need to read the output of the program while it's running and put that into a Swing textbox. But every way I've tried it is either unstable or simply does not work at all. Could anyone help me out with this? Note I have tried Apache Commons exec with little success. Also I'm trying to make this cross-platform and have created a method for getting the OS:
public enum OSType {
WINDOWS, OSX, LINUX, DAFUQ
}
/**
* Gets OS running on then caches result
* @return OS running on
**/
public static OSType getOS() {
if (os != null)
return os;
else {
os = getOperatingSystem();
return os;
}
}
/** Don't use this! Use {@link Frame#getOS()} instead **/
public static OSType getOperatingSystem() {
String OS = System.getProperty("os.name").toLowerCase();
if (OS.indexOf("win") >= 0)
return OSType.WINDOWS;
else if (OS.indexOf("mac") >= 0)
return OSType.OSX;
else if (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0)
return OSType.LINUX;
else
return OSType.DAFUQ;
}
EDIT:
To clarify, I am creating a .jar program that acts as a gui for a EXE. This is the code that I am currently trying to use to start the program:
ProcessBuilder prc = new ProcessBuilder(commands);
prc.redirectErrorStream(true);
try {
Frame.exec = prc.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
The commands
variable is just a list for the commands I need to execute. The program is running correctly because I can see it working on the task manager. Here is also the code for how I am reading from the InputStream of the process:
BufferedReader in = new BufferedReader(new InputStreamReader(exec.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
I am trying to read from the stream asynchronously, so this code is in another thread, which is why I needed to store the "exec" variable.