读取java exec的输出

时间:2011-11-16 10:07:52

标签: java runtime runtime.exec

你好我对java有一些疑问。 这是我的代码:

public static void main(String[] args) throws Exception {
    Process pr = Runtime.getRuntime().exec("java -version");

    BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    pr.waitFor();
    System.out.println("ok!");

    in.close();
    System.exit(0);
}

在那个代码我试图获得一个java版本命令执行是好的,但我无法读取输出它只返回null。为什么呢?

5 个答案:

答案 0 :(得分:50)

使用getErrorStream()

BufferedReader in = new BufferedReader(new InputStreamReader(pr.getErrorStream()));

编辑:

您可以使用ProcessBuilder(并阅读文档)

ProcessBuilder   ps=new ProcessBuilder("java.exe","-version");

//From the DOC:  Initially, this property is false, meaning that the 
//standard output and error output of a subprocess are sent to two 
//separate streams
ps.redirectErrorStream(true);

Process pr = ps.start();  

BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
    System.out.println(line);
}
pr.waitFor();
System.out.println("ok!");

in.close();
System.exit(0);

答案 1 :(得分:9)

请注意,我们正在逐行读取流程输出到StringBuilder。由于try-with-resources语句,我们不需要手动关闭流。 ProcessBuilder类允许我们向其构造函数提交程序名称和参数数量。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ProcessOutputExample
{
    public static void main(String[] arguments) throws IOException,
            InterruptedException
    {
        System.out.println(getProcessOutput());
    }

    public static String getProcessOutput() throws IOException, InterruptedException
    {
        ProcessBuilder processBuilder = new ProcessBuilder("java",
                "-version");

        processBuilder.redirectErrorStream(true);

        Process process = processBuilder.start();
        StringBuilder processOutput = new StringBuilder();

        try (BufferedReader processOutputReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));)
        {
            String readLine;

            while ((readLine = processOutputReader.readLine()) != null)
            {
                processOutput.append(readLine + System.lineSeparator());
            }

            process.waitFor();
        }

        return processOutput.toString().trim();
    }
}

打印:

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

答案 2 :(得分:2)

您已拥有process - 对象(名称为pr)。您可以获得Input-,Output-和Errorstream。在您的情况下,您需要pr.getInputStream()。从那里读取,它连接到过程的输出。

答案 3 :(得分:0)

试试这个

public static final Pair<Integer,Integer> javaVersion(File file) throws IOException {
    final ProcessBuilder pb = new ProcessBuilder("java", "-version");
    pb.directory(new File(file.getCanonicalPath() + File.separator  + "bin"));
    pb.redirectErrorStream(true);

    // Call the test target
    final Process process = pb.start();
    final InputStream in = process.getInputStream();
    final InputStream err = process.getErrorStream();

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
    String s = bufferedReader.readLine();
    int start = s.indexOf('\"');
    int end = s.lastIndexOf('\"');
    String substring = s.substring(start + 1, end);
    String[] split = substring.split("\\.");

    return new Pair<>(Integer.parseInt(split[0]),Integer.parseInt(split[1]));
}

答案 4 :(得分:0)

我也遇到了这个问题,因为我没有正确设置$ JAVA_HOME。 (我忘了目录/首页)。
编辑$ JAVA_HOME之后,更新Gradle JVM,并删除.idea目录以使用gradle重新构建,效果很好。