【问题标题】:Run Python script through Java?通过 Java 运行 Python 脚本?
【发布时间】:2015-10-06 08:34:25
【问题描述】:

我正在通过 java 运行一个 python 脚本。下面提供了我目前拥有的部分代码,并且成功运行了一个不需要用户输入的 python 脚本。它当前正在显示我在 python 脚本中指示的要在终端中打印的任何内容。我正在通过 mac oxs 终端执行此操作。但是,每当我运行需要用户输入的 python 脚本时,它不会在 python 脚本的“输入”语句之后显示任何内容。 我需要它来处理来自 python 脚本的用户输入。

请帮我解决这个问题,谢谢!

public static void main(String[] args) {
    test obj = new test();

    //in mac oxs
    String command = "python testLOL.py";
    String output = obj.executeCommand(command);
    System.out.println(output);     

}

private String executeCommand(String command) {

    StringBuffer output = new StringBuffer();

    Process p;
    try {
        Runtime r = Runtime.getRuntime();
        p = r.exec(command);
        p.waitFor();
        BufferedReader reader = 
        new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";           
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }

        p.waitFor();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return output.toString();   
}

【问题讨论】:

  • 看来您必须以某种方式将 System.out 和 System.in 绑定到进程
  • @kamil09875 更像 System.in 到 p.getOutputStream

标签: java python input terminal


【解决方案1】:

您可以创建两个线程将两个流绑定到进程

public static void main(final String[] args){
    executeCommand("python test/test.py");
}

private static void executeCommand(final String command){
    final Process p;
    try{
        p = Runtime.getRuntime().exec(command);

        Thread bindIn = new Thread(() -> {
            try{
                int av;
                while(!Thread.interrupted())
                while((av = System.in.available()) > 0){
                    byte[] bytes = new byte[av];
                    System.in.read(bytes);
                    p.getOutputStream().write(bytes);
                    p.getOutputStream().flush();
                }

                System.out.println("bindIn ended");
            }catch(Exception e){
                e.printStackTrace();
                return;
            }
        });

        Thread bindOut = new Thread(() -> {
            try{
                int av;
                while(!Thread.interrupted())
                while((av = p.getInputStream().available()) > 0){
                    byte[] bytes = new byte[av];
                    p.getInputStream().read(bytes);
                    System.out.write(bytes);
                }

                System.out.println("bindOut ended");
            }catch(Exception e){
                e.printStackTrace();
                return;
            }
        });

        bindIn.start();
        bindOut.start();

        p.waitFor();

        bindIn.interrupt();
        bindOut.interrupt();
    }catch(Exception e){
        e.printStackTrace();
    }
}

请注意,现在您不需要在最后读取输出,因为它是即时传输的。

在您的代码中,您执行的进程具有自己的流,因此写入 System.in 并不会导致写入 p.getOutputStream(),反之亦然。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多