【问题标题】:Process inputstream is not returning process input (OSX)进程输入流不返回进程输入(OSX)
【发布时间】:2017-06-15 01:51:55
【问题描述】:

我正在尝试获取我使用流程构建器启动的流程的输出,但是输出看起来像是来自 applescript 的东西,而不是流程的实际输出:

代码:

public static void main(String[] args) {
    System.out.println(getJavaOutput());
}

public static String getJavaOutput() {
    try {
        //kill -9 $(ps -p $PPID -o ppid=)
        String output = "";
        ProcessBuilder macBuilder = new ProcessBuilder("osascript", "-e",
                "tell application \"Terminal\" to do script \"jps -lV && exit\"");
        Process p2 = macBuilder.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p2.getInputStream()));
        String line;
        while((line = reader.readLine()) != null){
            output += line + System.getProperty("line.separator");
        }
        return output;
    } catch (Exception e) {
        e.printStackTrace();
    }   
    return null;
}

预期输出:

2680 sun.tools.jps.Jps
1289 

实际输出:

tab 1 of window id 742

【问题讨论】:

    标签: java macos process terminal


    【解决方案1】:

    将您的方法更改为:

    public static String getJavaOutput() {
        try {
            String output = "";
            ProcessBuilder macBuilder = new ProcessBuilder("/bin/bash", "-c", "jps -lV && exit");
            Process p2 = macBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p2.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                output += line + System.getProperty("line.separator");
            }
            return output;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 2013-10-17
      • 2016-01-29
      • 1970-01-01
      相关资源
      最近更新 更多