从 Java 运行 .py 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27267391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:04:09  来源:igfitidea点击:

Running a .py file from Java

javapython

提问by Jose Ramon

I am trying to execute a .py file from java code. I move the .py file in the default dir of my java project and I call it using the following code:

我正在尝试从 java 代码执行 .py 文件。我将 .py 文件移动到我的 java 项目的默认目录中,并使用以下代码调用它:

    String cmd = "python/";
    String py = "file";
    String run = "python  " +cmd+ py + ".py";
    System.out.println(run);
    //Runtime.getRuntime().exec(run);

    Process p = Runtime.getRuntime().exec("python  file.py");

Either using variable run, or the whole path or "python file.py" my code is running showing the message build successful total time 0 seconds without execute the file.py. What is my problem here?

使用变量运行,或整个路径或“python file.py”,我的代码正在运行,显示消息构建成功总时间为 0 秒,而不执行 file.py。我这里有什么问题?

采纳答案by Prateek

You can use like this also:

你也可以这样使用:

String command = "python /c start python path\to\script\script.py";
Process p = Runtime.getRuntime().exec(command + param );

or

或者

String prg = "import sys";
BufferedWriter out = new BufferedWriter(new FileWriter("path/a.py"));
out.write(prg);
out.close();
Process p = Runtime.getRuntime().exec("python path/a.py");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String ret = in.readLine();
System.out.println("value is : "+ret);

Run Python script from Java

从 Java 运行 Python 脚本

回答by Maddy

I believe we can use ProcessBuilder

我相信我们可以使用 ProcessBuilder

Runtime.getRuntime().exec("python "+cmd + py + ".py");
.....
//since exec has its own process we can use that
ProcessBuilder builder = new ProcessBuilder("python", py + ".py");
builder.directory(new File(cmd));
builder.redirectError();
....
Process newProcess = builder.start();

回答by anubhav maity

You can run the python script

你可以运行python脚本

Process p = Runtime.getRuntime().exec(PYTHON_ABSOLUTE_PATH, script_path)

To get the PYTHON_ABSOLUTE_PATH just type

要获得 PYTHON_ABSOLUTE_PATH 只需键入

which python2.7

in terminal

在终端

回答by Anshul Gupta

String command = "cmd /c python <command to execute or script to run>";
    Process p = Runtime.getRuntime().exec(command);
    p.waitFor();
    BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
          String line;
        while ((line = bri.readLine()) != null) {
            System.out.println(line);
          }
          bri.close();
          while ((line = bre.readLine()) != null) {
            System.out.println(line);
          }
          bre.close();
          p.waitFor();
          System.out.println("Done.");

    p.destroy();

回答by mannedear

Though the OP has got the answer, I'm posting my solution which might help someone else like me..

尽管 OP 得到了答案,但我正在发布我的解决方案,这可能会帮助像我这样的其他人..

        File file = new File(("C:/.../file.py"));

        List<String> list = new ArrayList<String>();
        list.add("python.exe");
        String absPath = file.getAbsolutePath();

        System.out.println("absPath>>"+absPath);

        list.add(absPath);
        ProcessBuilder pb = new ProcessBuilder(list);
        Process process = pb.start();

        InputStream inputStream = process.getInputStream();
        byte[] b = new byte[1024 * 1024];// {(byte) 1024};
        while (inputStream.read(b) > 0) {
            System.out.println("b.length>>"+new String(b));
        }

        process.waitFor();
        System.out.println("exitValue()>>"+process.exitValue());    //Should return 0 on successful execution