• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python 如何安装py4j

python 搞java代码 3年前 (2022-05-21) 39次浏览 已收录 0个评论


Mac安装:

pip 安装

pip install py4j
py4j.<a href="https://www.gaodaima.com/tag/jar" title="查看更多关于jar的文章" target="_blank">jar</a>包路径:/usr/<a href="https://www.gaodaima.com/tag/share" title="查看更多关于share的文章" target="_blank">share</a>/py4j/py4j0.x.jar or /usr/local/share/py4j/py4j0.x.jar

www#gaodaima.com来源gaodai#ma#com搞*!代#%^码网搞代码

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之后)

搞代码网,免费的在线学习python平台,欢迎关注!

来源:搞代码网:原文地址:https://www.gaodaima.com


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python 如何安装py4j

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址