正在 java 中执行进程以调用外部 python 程序,但程序不会向控制台打印任何内容

Executing Process in java to call external python program but program does not print anything to console

我们可以使用 Jython 在 java 中实现 python,但我不想采用这种方法,我正在寻找的是使用命令行实用程序并启动 python命令执行代码并在 java 代码中获取控制台输出。

python Main.py < input.txt

我在终端中使用了上面的命令,它在那里工作,给我输出,但无法在 java 代码中获得输出。

注意:Main.py和input.txt和java代码在同一个文件夹

我在 java 代码中做错了什么?

这是示例 java 代码,我调用它来执行外部 python 代码

try {
    Process process = Runtime.getRuntime()
            .exec("python Main.py < input.txt");
    
        process.waitFor();
        System.out.println(process);
        StringBuilder output
        = new StringBuilder();

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

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

        int exitVal = process.waitFor();
        if (exitVal == 0) {
            System.out.println("Success!");
            System.out.println(output);
        } else {
            System.out.println("Process failed");
        }
} catch (Exception e) {
    // TODO: handle exception
    System.out.println(e);
}

这是一个示例 python 代码:

x = input();
y = input();
print(type(x));
print(type(y));
print(x + y);

这是一个示例输入文件,我将其作为输入传递给 python 代码

30
40

java 进程不接受 < 符号以在 python 命令中输入文件。 相反,你可以 运行 像这样 python 文件

    f = open("input.txt", "r")
    for x in f:
    print(type(x));
    print(x)

java 文件

        Process process = Runtime.getRuntime().exec("python Main.py  input.txt");
        process.waitFor();
        System.out.println(process);
        StringBuilder output
                = new StringBuilder();

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));

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

        int exitVal = process.waitFor();
        if (exitVal == 0) {
            System.out.println("Success!");
            System.out.println(output);
        } else {
            System.out.println("Process failed");
        }
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e);
    }

并使用相同的文本文件。 它应该在控制台打印

如 sandip 所示,在 java 中执行命令与通过 BASH 执行 运行 命令不同。 起初我试图执行 bash -c "python Main.py < input.txt"(通过 java)。 由于某种原因,这不起作用,即使它起作用也不是一个很好的解决方案,因为它依赖于其 运行 上的系统。 我发现可行的解决方案是使用 ProcessBuilder 首先发出命令,然后将其输入重定向到一个文件。这允许您保持 python 代码不变,至少对我来说,给出与 运行 BASH 命令相同的结果。 示例:

         ProcessBuilder pb = new ProcessBuilder("python3","Main.py");
         //Make sure to split up the command and the arguments, this includes options
         //I only have python3 on my system, but that shouldn't affect anything
         pb.redirectInput(new File("./input.txt"));
         System.out.println(pb.command());
         Process process = pb.start();
         //The rest is the exact same as the code in the question

Heres the ProcessBuilder docs for quick reference