0

In windows, use Java, mostly we can call Runtime.getRuntime().exec to execute an executable application or batch file, then call proc.getErrorStream() proc.getInputStream() to get the standard output/error stream.

but in this time, I have an application called 'caption2ass.exe' (caption2ass.exe is a well knownd popular tool that can extract ass subtitle from Transport Stream), it prints a lot of information into the screen, but it seems that Java program CAN NOT receive the information by calling proc.getErrorStream() or proc.getInputStream().

Manually I typed 'caption2ass.exe' in the command line, and then I pressed [enter]. after that, the screen will show:

manually I entered caption2ass.exe in the cmd

I am trying to receive The infomation in the screen and put it into sysout, or put it into an string array in future.

My Java code is as below:

main program:

    String cmd = "E:\\program_media\\Mikey's Fansub Utilities\\TS-OneKeyProcess\\tools\\caption2ass-pcr\\Caption2Ass_PCR.exe";
    Runtime run = Runtime.getRuntime();
    Process proc = run.exec(cmd);
    StreamGobbler errorGobbler = new StreamGobbler(
        proc.getErrorStream(), "GBK", "ERR", System.err);
    StreamGobbler outputGobbler = new StreamGobbler(
        proc.getInputStream(), "GBK", "OUT", System.out);
    errorGobbler.start();
    outputGobbler.start();
    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);

StreamGobbler.java :

public class StreamGobbler extends Thread {
    InputStream in;
    String charsetName;
    String type;
    PrintStream out;
    StreamGobbler(InputStream inputStream, String charsetName, String type, PrintStream out) {
        this.in = inputStream;
        this.charsetName = charsetName;
        this.type = type;
        this.out = out;
    }
    @Override
    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(in, charsetName);
            char[] cbuf = new char[256];
            int len = -1;
            while ( -1 != (len=isr.read(cbuf))){
                out.print(Arrays.copyOf(cbuf, len));
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
}

After running this java program, I only got a strange character before ExitValue: After running this java program, I only got a strange character

So, My question is: How to get the output information in the screen of this 'caption2ass.exe' using java?

you can get caption2ass from here : http://pan.baidu.com/s/1nuCClXR , you can run this program in your sandbox if you dare not run an unknown program, especially from Baidu.

Any tests are welcome.

qiangbro
  • 128
  • 1
  • 9
  • Possible duplicate of [java runtime.getruntime() getting output from executing a command line program](http://stackoverflow.com/questions/5711084/java-runtime-getruntime-getting-output-from-executing-a-command-line-program) – Rao Sep 19 '16 at 11:59
  • @Rao caption2ass is something special. I CAN NOT normally get the information by calling proc.getErrorStream() or proc.getInputStream(). – qiangbro Sep 19 '16 at 12:06
  • Are you sure it prints to the stderr? Did you try getOutputStream() for stdout? – ike3 Sep 19 '16 at 12:24
  • @ike3 it seems it doesn't prints anything to stderr, because I didn't receive any information from proc.getErrorStream() or proc.getInputStream(). but it still print a lot of information into the command line when I manually entered caption2ass.exe in the cmd. – qiangbro Sep 19 '16 at 12:53
  • You asked this same question a couple of days ago. Please don't ask the same question over and over. – David Heffernan Sep 19 '16 at 13:00
  • @DavidHeffernan you voted? please don't vote.... I want an answer. nice to meet you again. i have delete the early not good question. This time I have attached my code. – qiangbro Sep 19 '16 at 13:18
  • You know that you can replace all of your code (including all use of StreamGobbler) with `int exitVal = new ProcessBuilder("Caption2ass.exe").inheritIO().start().waitFor();`, right? Runtime.exec has been obsolete for years. – VGR Sep 19 '16 at 13:27
  • @VGR thank you, i have changed my code. but i still can't recieve the screen output. – qiangbro Sep 19 '16 at 13:35
  • Try running `Caption2Ass_PCR.exe >nul 2>nul` on the command line. If you still see output, the program is using neither standard out nor standard error. In that case, unless it has an option to redirect its output, there is nothing you can do. – VGR Sep 19 '16 at 18:28
  • @VGR I tried `Caption2Ass_PCR.exe >nul 2>nul` , and there is nothing output. – qiangbro Sep 20 '16 at 00:39
  • @VGR I tried `new ProcessBuilder("Caption2ass.exe").inheritIO().start().waitFo‌​r();` another time. it only print an strange character into eclipse console. but when I package it into a jar file, and run the jar in windows command line, it prints everything. i want to receive the information, so I want to use `getErrorStream`/`getOutputStream` to receive it. if use `inheritIO()`, I can't receive it. – qiangbro Sep 20 '16 at 08:31
  • @VGR it seems no one could answer this question yet. I have found the sourcecode of caption2ass, changed some code and recompiled it. so I can receive those information by calling `proc.getOutputStream()` now. – qiangbro Sep 21 '16 at 04:12

1 Answers1

0

谢谢各位的回复。

I have found the source code of caption2ass, it is a c++ program.

I have changed every logging statment, let them log information into stdout, and then recompiled it.

so i can easily receive the output of caption2ass in the java program, using proc.getInputStream() now.

qiangbro
  • 128
  • 1
  • 9