Mac安装:Mac安装:
pip 安装
pipinstalpy4j py4j.jar包路径:/usr/share/py4j/py4j0.x.jaror/usr/local/share/py4j/py4j0.x.jar
conda 安装(或安装Anaconda-Navigator)
condainstalpy4j py4j.jar包路径:/anaconda3//share/py4j/py4j0.x.jar
使用:
1、java代码
//需要导入py4j.jar包 importpy4j.GatewayServer; importjava.util.Random; publicclassEntryPoint{ privatestaticfinalRandomrandom=newRandom(); publicintrandInt(){ returnrandom.nextInt(); } publicstaticvoidmain(String[]args){ EntryPointapp=newEntryPoint(); //py4j服务 GatewayServergatewayServer=newGatewayServer(app); gatewayServer.start(); System.out.println("GatewayServerStarted"); } }
2、Python代码
frompy4j.java_gatewayimportJavaGateway #初始化 gateway=JavaGateway() defmain(): #在Java-EntryPoint中调用randint函数 a=gateway.entry_point.randInt() print(a) if__name__='__main__': main()
注意:
1.导入py4j.jar包
2.运行Java代码
3.运行Python代码(必须在2之后)
python学习网,免费在线学习python平台,欢迎关注!