Runtime exec doesn't show results

后端 未结 1 1565
臣服心动
臣服心动 2021-01-24 05:54

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 {
              


        
1条回答
  •  迷失自我
    2021-01-24 06:32

    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.

    0 讨论(0)
提交回复
热议问题