1

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.

  • Are you really saying: you are writing **one** java application (that you want to deploy as binary EXE); and your java code is writing to system.out; and now you want to do what? to fetch that output and put it into a UI element? – GhostCat Aug 08 '16 at 04:31
  • There's a standard pattern for this, and several dups on SO, but I can't seem to find them right now. The basic requirement is one thread to feed input to the process' `stdin`, one thread to read output from the process' `stdout/stderr`, and the main thread to clean up after the process exits. Use `ProcessBuilder` to launch the exernal executable. BTW, where's your code that actually launches the process? – Jim Garrison Aug 08 '16 at 04:50
  • http://stackoverflow.com/questions/8149828/read-the-output-from-java-exec?rq=1 ? –  Aug 08 '16 at 05:34
  • So I'm deploying a .jar file that runs an exe in a background then gets the console output AS ITS STILL RUNNING. I've tried to get from ProcessBuilder but it waits until the the program is closed before it can read the console output. I'll get the code in a sec but it really is incomplete since I've had to remove it to try other methods. – Daniel Pickering Aug 08 '16 at 22:55
  • I've updated it to clarify what I need to do. – Daniel Pickering Aug 08 '16 at 23:30
  • 1
    If the underlying launched executable doesn't flush the buffer, there's nothing you can do. Do you know whether it does? – David P. Caldwell Aug 08 '16 at 23:40

1 Answers1

0

I've tried to get from ProcessBuilder but it waits until the the program is closed before it can read the console output.

No it doesn't. It reads whatever is written as soon as it's written. What you're seeing is the effect of buffering output in the child process, probably by stdio. If you're not in control of the code of that process there is nothing you can do about it.

user207421
  • 305,947
  • 44
  • 307
  • 483