繁体   English   中英

如何从 Java 中的控制台应用程序中获取实时 output

[英]How to get Live output from a consol Application in Java

I have an EXE, written in python and then convert it into an exe, When i run the exe direclty i can see the output in console realtime, but when i run the exe in java i cannot see the output until the exe complete the process .

我在网上搜索但注意到工作正常,这是示例代码。

package com.imviewer.ai;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FaceAnalysis 实现 AILibrary { private static final Logger log = LoggerFactory.getLogger(FaceAnalysis.class);

private static final String KILL = "taskkill /F /IM ";
private static final String EXE_CPU = "FaceAnalysis-CPU.exe";
private static final String EXE_GPU = "FaceAnalysis-GPU.exe";

List<String> paths = new ArrayList<>();

public FaceAnalysis() {

}

@Override
public void setArguments() {

    log.info("Setting arguments");
    paths.add(new File(AIUtils.FACE, EXE_CPU).getAbsolutePath());
    paths.add("--images=yes");
    paths.add("--videos=yes");
    paths.add("--csv=no");
    paths.add("--folderpath=\"Face\"");
    paths.add("--algo=cnn");
    paths.add("--gpu=no");
    paths.add("--cpus=6");
    paths.add("--debug=no");
    paths.add("--fpstoproces=5");
    paths.add("--upsample=0");
    paths.add("--onlydetection=yes");
    paths.add("--savefullimages=no");
    paths.add("--savefaceimage=no");
    paths.add("--enablebox=no");
    paths.add("--maxarea=640000");
    paths.add("--imageextensions=jpg,png,jpeg,tiff,raw,bmp");
    paths.add("--videoextensions=mp4,avi,flv,mpeg,mpg,wmv");
    paths.add("--age=1");
    paths.add("--gender=1");
    paths.add("--expression=1");

}

@Override
public void run() {

}

@Override
public void runEXE() {
    log.info("Running Face Exe");

    ProcessBuilder builder = new ProcessBuilder(paths);
    builder.directory(new File(AIUtils.FACE));
    builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
    builder.redirectErrorStream(true);

    Process proc;
    try {
        proc = builder.start();
        proc.getOutputStream().close();

         // Any error message?
        Thread errorGobbler
          = new Thread(new StreamGobbler(proc.getErrorStream(), System.err));

        // Any output?
        Thread outputGobbler
          = new Thread(new StreamGobbler(proc.getInputStream(), System.out));

        errorGobbler.start();
        outputGobbler.start();

        // Any error?
        int exitVal = proc.waitFor();
        errorGobbler.join();   // Handle condition where the
        outputGobbler.join();  // process ends before the threads finish

        log.info("Exit Value: "+ exitVal);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    log.info("End Running Face EXE");
}

@Override
public boolean checkAlreadyRunning() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void close() {
    // TODO Auto-generated method stub

}

class StreamGobbler implements Runnable {
    private final InputStream is;
    private final PrintStream os;

    StreamGobbler(InputStream is, PrintStream os) {
        this.is = is;
        this.os = os;
    }

    public void run() {
        try {
            int c;
            while ((c = is.read()) != -1)
                os.print((char) c);
        } catch (IOException x) {
            // Handle error
        }
    }
}

public static void main(String[] args) {
    FaceAnalysis face = new FaceAnalysis();
    face.setArguments();
    face.runEXE();
}
}

试试这样的方法:

public static void main(String[] args) throws IOException {
    Process p = new ProcessBuilder().command("ping", "webelement.click").start();
    InputStream i = p.getInputStream();
    Scanner scanner = new Scanner(i);           
    while (scanner.hasNextLine()){              
        System.out.println(scanner.nextLine()); 
    }
}

暂无
暂无

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

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