• 技术文章 >Python技术 >Python基础教程

    python 如何安装py4j

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-12-31 11:44:39原创5646

    Mac安装:

    pip 安装

    1

    2

    pip install py4j

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

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

    1

    2

    conda install py4j

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

    使用:

    1、java代码

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    // 需要导入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代码

    1

    2

    3

    4

    5

    6

    7

    8

    9

    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学习网,免费的在线学习python平台,欢迎关注!

    专题推荐:python 安装 py4j
    上一篇:python如何自动加空格 下一篇:python总说的cd是什么

    相关文章推荐

    • python3如何安装webpy• python怎么安装numpy• python第三方库怎么安装• python怎样安装pil

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网