简体   繁体   English

Java-ProcessBuilder没有输出我的python文件

[英]Java - ProcessBuilder is not outputting my python file

Some context: I'm trying to make a GUI on java and have python code run once I click a button on the GUI. 一些上下文:我试图在Java上创建GUI,并在单击GUI上的按钮后运行python代码。 For example if I press start on the java GUI, it will run the python code on file .py. 例如,如果我在Java GUI上按启动键,它将在文件.py上运行python代码。

Why is it not outputting anything? 为什么不输出任何东西? You can also try this by creating a test python file and just inputting your own file location into the code below. 您也可以通过创建测试python文件,然后在下面的代码中输入您自己的文件位置来进行尝试。

Code so far: 到目前为止的代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.JFrame;

public class main_gui extends JFrame {
     public static void main(String[] args) throws Exception {

        ProcessBuilder builder = new ProcessBuilder("cmd.exe",
                "cd \"G:\\...Javaa\\filetranslatorapplication\\file_translator_app.py");
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
        }
    }
}

Two things to keep in mind: 请记住两件事:

  1. The python file in NOT in the same place as this java file. python文件不在此java文件的同一位置。
  2. The java file and python file are both on a usb, hence the "G:\\..". Java文件和python文件都位于USB上,因此为“ G:\\ ..”。

Also, if anyone has a better way of running python code through java gui, please feel free to point me in the right direction. 另外,如果有人有更好的方法通过java gui运行python代码,请随时向我指出正确的方向。

Your process simply does not make sense. 您的过程根本就没有意义。

It simply spawn a cmd , and cd to an invalid directory (as it is a file). 它只是生成一个cmd ,然后将cd生成一个无效目录(因为它是一个文件)。

What you want is probably 你想要的可能是

cmd /c python g:\your\path\foo.py

or 要么

cmd /c g:\your\path\foo.py

or simply 或简单地

may\be\full\path\is\needed\python g:\your\path\foo.py

So, your code should look like: 因此,您的代码应如下所示:

ProcessBuilder builder 
    = new ProcessBuilder("cmd.exe", "/c", "python", "g:\\yourpath\\file_translator_app.py");
// or 
// = new ProcessBuilder("python", "g:\\yourpath\\file_translator_app.py");
// or 
// = new ProcessBuilder("cmd", "/c", "g:\\yourpath\\file_translator_app.py");

The following should work for you: 以下应该为您工作:

    ProcessBuilder builder = new ProcessBuilder("cmd",
            "/c \"G: && python Javaa\\filetranslatorapplication\\file_translator_app.py\"");

This gets executed when I run my Java application from a different drive. 当我从其他驱动器运行Java应用程序时,将执行此操作。 But, for you, if it is the same drive, need not switch to G: . 但是,对于您来说,如果是同一驱动器,则无需切换到G: You may execute the py file with python command. 您可以使用python命令执行py文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM