Java中Runtime.getRuntime.exec()的空输出

时间:2018-07-30 12:11:51

标签: java runtime.exec

我正在尝试使用Java中的循环执行一系列JDEPS命令。我已经使用Process对象捕获命令([[Runtime.getRuntime.exec("command")] [1])的执行,并使用[BufferedReader] [2]来获取过程输入和错误流。但是输出为空。也没有错误代码。我已经在终端上手动运行了命令,它运行正常。

有人可以建议如何找出问题所在,还是为什么会出现空输出?

请在下面找到我用于执行命令的代码:

private String[] runCommandByType(String command,int type)
{
    String s=null;
    ArrayList<String> output = new ArrayList<String>();

    try {

        // using the Runtime exec method:
        Process p = Runtime.getRuntime().exec(command);
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        // read the output from the command
        while ((s = stdInput.readLine()) != null) {
            if(type==0) //takes in all the output
            {
                output.add(s);
            }
            if(type==1) //takes only the dependent class names
            {
                s=s.substring(0, s.indexOf("->")).replaceAll("\\s+","");
                output.add(s);
            }
        }
        // read any errors from the attempted command
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }
    }
    catch (IOException e) {
        System.out.println("$$$$ Exception happened $$$$");
        e.printStackTrace();
    }
    return output.toArray(new String[output.size()]);
}

谢谢!

0 个答案:

没有答案