我想使用Py4J库从python调用Java,

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

我收到以下错误:“Py4JNetworkError:尝试连接到Java服务器时发生错误”。似乎没有JVM正在运行,如何解决该问题?

最佳答案

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();
      }
}

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

09-27 18:02