简体   繁体   English

Java-在程序仍在运行时使用exec并读取输出

[英]Java - Use exec and read output while program is still running

So I'm creating a gui for an exe in Java. 因此,我正在为Java中的exe创建gui。 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. 由于此程序是一个控制台程序,因此我需要在程序运行时读取其输出,并将其放入Swing文本框中。 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. 注意我尝试了Apache Commons exec,但收效甚微。 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. 为了澄清,我正在创建一个.jar程序,用作EXE的gui。 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. commands变量只是我需要执行的命令的列表。 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: 这也是我如何从流程的InputStream中读取代码:

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. 我正在尝试从流中异步读取,因此此代码在另一个线程中,这就是为什么我需要存储“ exec”变量。

I've tried to get from ProcessBuilder but it waits until the the program is closed before it can read the console output. 我试图从ProcessBuilder获取,但是它一直等到程序关闭后才能读取控制台输出。

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 . 您所看到的是缓冲子进程中输出的效果,可能是stdio If you're not in control of the code of that process there is nothing you can do about it. 如果您无法控制该过程的代码,那么您将无能为力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM