【问题标题】:Running a Python program in Java using Jython使用 Jython 在 Java 中运行 Python 程序
【发布时间】:2021-03-22 12:11:47
【问题描述】:

我编写了一个由五个 .py 脚本文件组成的 Python 程序。 我想从 Java 应用程序中执行这些 python 脚本的主要部分。

我有什么选择这样做?使用 PythonInterpreter 不起作用,例如无法从 Jython 加载 datetime 模块(我不希望用户确定他的 Python 路径以使这些依赖项正常工作)。

我使用 Jython 的 compileall 将整个文件夹编译为 .class 文件。我可以以某种方式嵌入这些 .class 文件以从我的 Java 应用程序中执行主文件,或者我应该如何继续?

【问题讨论】:

  • 我认为 Jython 是为了让您可以将 Java 工具包含到 Python 中,而不是相反...
  • @cricket_007 Jython 是 JVM 的 Python 实现。您可以主要使用任何一种语言进行编程并进行互操作。
  • 如何从 java 调用 .vbs 文件并让该 .vbs 文件调用 python 文件。

标签: java python jython


【解决方案1】:

看看 java 中的 ProcessBuilder 类:https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html

java 构造函数中使用的命令应该与您在命令行中键入的命令相同。例如:

Process p = new ProcessBuilder("python", "myScript.py", "firstargument").start();

(流程构建器与 python subprocess 模块做同样的事情)。

看看running scripts through processbuilder

注意至于问题的 Jython 部分,如果您访问 jython 网站(查看他们网站 www.jython.org 的常见问题解答部分)。检查条目“使用 java 中的 jython”。

【讨论】:

  • 在这种情况下,我需要确保用户安装了 Python 2.x 才能使我的脚本正常工作,它已注册为“python”命令,最后我需要提取硬盘驱动器上某处的脚本。这不是我想要的,我会看看 Jython。
  • 虽然有用的信息,但这似乎并不能直接回答使用来自 Java/Jython 的 Python 代码的问题,绕过安装 Python 运行时的需要。请看我的post
【解决方案2】:

我也有兴趣直接 Java 中运行 Python 代码,使用 Jython,并避免安装 Python 解释器。

文章“Embedding Jython in Java Applications”解释了如何引用外部*.py Python 脚本,并为其传递参数,没有需要安装 Python 解释器:

#pymodule.py - make this file accessible to your Java code
def square(value):
return value*value

然后可以通过创建一个字符串来执行此函数 执行它,或者通过检索指向函数的指针并调用 它的 call 方法带有正确的参数:

//Java code implementing Jython and calling pymodule.py
import org.python.util.PythonInterpreter;
import org.python.core.*;

public class ImportExample {
   public static void main(String [] args) throws PyException
   {
       PythonInterpreter pi = new PythonInterpreter();
       pi.exec("from pymodule import square");
       pi.set("integer", new PyInteger(42));
       pi.exec("result = square(integer)");
       pi.exec("print(result)");
       PyInteger result = (PyInteger)pi.get("result");
       System.out.println("result: "+ result.asInt());
       PyFunction pf = (PyFunction)pi.get("square");
       System.out.println(pf.__call__(new PyInteger(5)));
   }
}

【讨论】:

  • 我的上述答案更符合在 Python 脚本中执行特定函数的要求。 stackoverflow.com/a/36288406/304330 的答案明确解释了如何通过 Jython 将参数传递给 Python 脚本,就像您可能从 CLi 中一样(不了解脚本的内部结构,只了解命令行)。我可能会修改我的答案以反映这些新信息。
【解决方案3】:

可以加载其他模块。您只需要指定可以找到自定义模块的 python 路径。请参阅以下测试用例,我在调用函数 (my_maths()) 中使用 Python 数据时间/数学模块,并且我在 python.path 中有多个由 main.py 导入的 python 文件

@Test
public void testJython() {

    Properties properties = System.getProperties();
    properties.put("python.path", ".\\src\\test\\resources");
    PythonInterpreter.initialize(System.getProperties(), properties, new String[0]);

    PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.execfile(".\\src\\test\\resources\\main.py");

    interpreter.set("id", 150); //set variable value
    interpreter.exec("val = my_maths(id)"); //the calling function in main.py

    Integer returnVal = (Integer) interpreter.eval("val").__tojava__(Integer.class);
    System.out.println("return from python: " + returnVal);
}

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多