Py4j无法连接到Java服务器 [英] Py4j Cannot connect to Java Server

查看:395
本文介绍了Py4j无法连接到Java服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个简单的程序来使用py4j在python和java之间建立连接.我写了以下两行,希望所有内容都能正常运行,因为我没有进行任何更改

I was trying to write a simple program to setup a connection between python and java using py4j. I wrote the following two lines hoping that everything would run since I'm not making any changes

from py4j.java_gateway import JavaGateway, GatewayParameters
gateway = JavaGateway(gateway_parameters=GatewayParameters(port=25335))
random = gateway.jvm.java.util.Random()

这导致了以下错误

py4j.protocol.Py4JNetworkError:尝试连接到Java服务器时发生错误(127.0.0.1:25335)

py4j.protocol.Py4JNetworkError: An error occurred while trying to connect to the Java server (127.0.0.1:25335)

我环顾了一会儿,读到如果Java正在侦听其他端口,则可能会发生这种情况.所以我写了一个for块,看看会发生什么

I looked around for a while and read that this might happen if java is listening on to a different port. So I wrote a for block to see what happens

for i in range(65535+1):
    try:
        gateway = JavaGateway(gateway_parameters=GatewayParameters(port=i))
        random = gateway.jvm.java.util.Random()
        print("passed port num", str(i))
    except:
        pass

上面的块没有产生任何结果.没有端口可以连接.我不知道我要去哪里.

The above block yielded nothing. None of the ports could connect. I can't figure out where I am going wrong.

如何找到Java端正在使用的端口号?

How do I find the port number which the java side is using?

我正在使用py4j版本0.10.7和python 3.6.0

I am using py4j version 0.10.7 and python 3.6.0

编辑

我使用了与 py4j教程

我有一个名为java_stack.javapy4j_gs.java的文件.两者都在同一个目录中.我正在从终端调用`java py4j_gs.java.这是两个文件的内容

I have a file called java_stack.java and py4j_gs.java. Both are in the same directory. I'm calling `java py4j_gs.java from the terminal. These are the contents of the two files

java_stack.java

package py4j.examples;    
import py4j.GatewayServer;
public class StackEntryPoint {
    private Stack stack;
    public StackEntryPoint() {
      stack = new Stack();
      stack.push("Initial Item");
    }
    public Stack getStack() {
        return stack;
    }
    public static void main(String[] args) {
        GatewayServer gatewayServer = new GatewayServer(new StackEntryPoint());
        gatewayServer.start();
        System.out.println("Gateway Server Started");
    }    
}

py4j_gs.java

package py4j.examples;
import py4j.GatewayServer;
public class StackEntryPoint {
    private Stack stack;
    public StackEntryPoint() {
      stack = new Stack();
      stack.push("Initial Item");
    }
    public Stack getStack() {
        return stack;
    }
    public static void main(String[] args) {
        GatewayServer gatewayServer = new GatewayServer(new StackEntryPoint());
        gatewayServer.start();
        System.out.println("Gateway Server Started");
    }
}

推荐答案

我也遇到过类似的问题

py4j.java_gateway:尝试连接到Java服务器时发生错误

py4j.java_gateway:An error occurred while trying to connect to the Java server

在使用pyspark shell时. 为了解决这个问题,我们首先需要启动java geteway服务器. 执行以下Java代码后,我的问题得到解决.

while working on pyspark shell. To solve this first we need to start the java geteway server. After executing following java code my problem got resolve.

import py4j.GatewayServer;

public class Test {

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

这篇关于Py4j无法连接到Java服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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