簡體   English   中英

如何使用java打印命令行執行輸出

[英]How to print command line execution output using java

在以下代碼提示符下運行時,將打開並顯示 java 版本信息。 我想打印那個java版本信息。 請幫助我

public class Test {

    public static void main(String[] args) throws IOException {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec("cmd.exe /c start cmd.exe /k \"java -version\"");
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        // Read the output from the command
        System.out.println("Here is the standard output of the command:\n");
        String s = null;
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }

    }

}

在您閱讀流的那一刻,該過程實際上可能尚未完成。 我很確定您首先必須等待int exitVal = proc.waitFor(); 在讀取 outputStream 之前完成的過程。

在嘗試登錄proc.getErrorStream()時調試包裝部分可能有助於

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM