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

在java中执行进程以调用外部python程序,但程序不会将任何内容打印到控制台,java,linux,spring,spring-boot,process,Java,Linux,Spring,Spring Boot,Process,我们可以使用Jython在java中实现python,但我不想采用这种方法,我要寻找的是使用命令行实用程序和firepython命令来执行代码并获得java代码中的控制台输出 python Main.py

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

python Main.py

我在终端中使用了上面的命令,它在那里工作,给我输出,但无法用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);
}
30
40
下面是一个示例输入文件,我将其作为输入传递给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);
}
30
40

java进程不接受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
(通过java)。 由于某些原因,这不起作用,即使它起作用,但它也不是一个很好的解决方案,因为它依赖于它所运行的系统。 我发现可行的解决方案是使用ProcessBuilder首先生成命令,然后将其输入重定向到文件。这允许您保持python代码不变,至少对我来说,结果与运行BASH命令相同。 例如:


这是否回答了您的问题?非常感谢,这是工作拯救了我的一天!感谢sandip代码正在工作,但我的用例是我不能修改python代码,只有java代码可以修改以执行python程序