Jython 是一个将 Python 代码编译成 Java 字节码的工具,允许在 Java 环境中直接运行 Python 代码。
Java 项目中,使用 Jython 的 API 来调用 Python 代码。例如:
import org.python.util.PythonInterpreter;
public class PythonModelCaller {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("your_python_model_code.py");
// 假设Python代码中定义了一个predict函数
interpreter.exec("result = predict('我想要一个CPU散热器')");
Object result = interpreter.get("result");
System.out.println(result);
interpreter.close();
}
}