python 如何安装py4j

615次阅读
没有评论

python

Mac安装:

pip 安装

pip install py4j
py4j.jar包路径:/usr/share/py4j/py4j0.x.jar or /usr/local/share/py4j/py4j0.x.jar

conda 安装(或者使用Anaconda-Navigator安装)

conda install py4j
py4j.jar包路径:/anaconda3/share/py4j/py4j0.x.jar

使用:

1、java代码

// 需要导入py4j.jar包
import py4j.GatewayServer;
import java.util.Random;
public class EntryPoint {
    private static final Random random = new Random();
    public int randInt() {
        return random.nextInt();
    }
    public static void main(String[] args) {
        EntryPoint app = new EntryPoint();
        // py4j服务
        GatewayServer gatewayServer = new GatewayServer(app);
        gatewayServer.start();
        System.out.println("Gateway Server Started");
    }
}

2、Python代码

from py4j.java_gateway import JavaGateway
# 初始化
gateway = JavaGateway()
def main():
    # 调用Java-EntryPoint中的randInt函数
    a = gateway.entry_point.randInt()
    print(a)
if __name__ == '__main__':
    main()

注意:

1.导入py4j.jar包

2.运行Java代码

3.运行Python代码(必须在2之后)

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2020-01-24发表,共计926字。
新手QQ群:570568346,欢迎进群讨论 Python51学习
评论(没有评论)