如何使用PY4J从python中调用java [英] How to call java from python using PY4J

查看:816
本文介绍了如何使用PY4J从python中调用java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Py4J库从python调用java,

I want to call java from python with Py4J library,

from py4j.java_gateway import JavaGateway
gateway = JavaGateway()                        # connect to the JVM
gateway.jvm.java.lang.System.out.println('Hello World!')

我遇到以下错误:Py4JNetworkError:尝试连接到Java服务器时发生错误。似乎没有JVM在运行,如何修复?

I've got the following error: "Py4JNetworkError: An error occurred while trying to connect to the Java server". It's seems that no JVM is running, how to fix that?

推荐答案

package test.test;

import py4j.GatewayServer;

public class AdditionApplication {
    public int addition(int first, int second) {
        return first + second;
      }

      public static void main(String[] args) {
        AdditionApplication app = new AdditionApplication();
        // app is now the gateway.entry_point
        GatewayServer server = new GatewayServer(app);
        server.start();
      }
}

创建一个新类并运行它(导入py4j0。 8.jar在'py4j-0.8\py4j-0.8\py4j-java'第一个),然后运行python程序

create a new class and run it(import py4j0.8.jar at 'py4j-0.8\py4j-0.8\py4j-java' first),then run python program

这篇关于如何使用PY4J从python中调用java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆