Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

2.2. Java 嵌入 Python 代码

		
// JDK 26 + Project Detroit
public class PythonIntegration {
    public static void main(String[] args) {
        try (var python = PythonRuntime.newRuntime()) {
            // 直接调用numpy等Python库
            PythonObject result = python.eval("""
                import numpy as np
                a = np.array([1,2,3,4,5])
                a.mean()
                """);
            System.out.println("均值: " + result.toDouble());
        }
    }
}