Runtime exec doesn't show results

我是研究僧i 提交于 2019-12-20 05:19:17

问题


I'm trying to get the result of a java command in netbeans, using the codes below:

    Runtime rt = Runtime.getRuntime();
    Process p = null;

    try {
        String cmd = "javac";
        // OR String cmd = "cmd /c javac";
        // OR String cmd = "java -cp myjar com.my.sample";
        p = rt.exec(cmd);            
        p.waitFor();

        BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream())); 
        String line = null;

        while ((line = br.readLine()) != null) {
            System.out.println(line);               
        }

    } catch(Exception e){
            //
    }

and expects to have the following output but it doesn't show anything in console.

Usage: javac  
where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings

java -cp my.jar com.my.sample

 Hello Sample Text

回答1:


There are two system dependent things.

First of all, you are waiting for the process’ completion via p.waitFor() before reading the program’s output. This may block forever when the pipe’s capacity is exhausted, as the program can only complete when it has written all of its output. The pipe’s actual capacity is system dependent and unspecified.

Further, since providing no arguments is an erroneous situation, it might be the case that the message has been written to the error stream instead of the standard output. In that case, you would have to read p.getErrorStream() instead of p.getInputStream() (on my system, this was not the case).

So, you may try

Runtime rt = Runtime.getRuntime();
try {
    String cmd = "javac";
    // OR String cmd = "cmd /c javac";
    // OR String cmd = "java -cp myjar com.my.sample";
    Process p = rt.exec(cmd);
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); 

    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }

    p.waitFor();
} catch(Exception e){
    e.printStackTrace();
}

And if it completes without printing something, try p.getErrorStream() instead of p.getInputStream().

But if you just want to print the subprocess’ output to the console, you can do it much better:

try {

    String cmd = "javac";
    // OR String cmd = "cmd /c javac";
    // OR String cmd = "java -cp myjar com.my.sample";
    Process p = new ProcessBuilder(cmd).inheritIO().start();
    p.waitFor();
} catch(Exception e){
    e.printStackTrace();
}

This connects your input and output channels to the subprocess’ input and output channels. So you don’t need to process a pipe for this case.

But if you actually want process the output in your program, i.e. the printing was only an example, ProcessBuilder still has an interesting option:

try {
    String cmd = "javac";
    // OR String cmd = "cmd /c javac";
    // OR String cmd = "java -cp myjar com.my.sample";
    Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start();
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); 

    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }

    p.waitFor();
} catch(Exception e){
    e.printStackTrace();
}

By using redirectErrorStream(true), you tell it to write any error output into the same channel as the standard output, so there’s only one output to handle.



来源:https://stackoverflow.com/questions/51167343/runtime-exec-doesnt-show-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!