使用 Eclipse 导入 py4j

Importing py4j using Eclipse

我已经阅读了 py4j.org 介绍的第一部分,然后我跳到了 Eclipse 部分。我安装了在这里找到的 Eclipse 插件:http://eclipse.py4j.org/ 然后重新启动了 Eclipse。

我在预先存在的 Java 项目中有一个 class,称为 DateRange,因此我根据说明创建了一个名为 DateRangeEntryPoint 的新 class。这由以下代码组成。

package statresearch.programs.DaypartParser;

import statresearch.programs.util.DateRange;
import py4j.GatewayServer;

public class DateRangeEntryPoint {


    private DateRange dateRange;

    public DateRangeEntryPoint(String startDate, String endDate, boolean     includeStart, boolean includeEnd) {
    dateRange = new DateRange(startDate, endDate, includeStart, includeEnd);
}

public DateRange getDateRange() {
    return dateRange;
}


public static void main(String[] args) {
    // TODO Auto-generated method stub
    GatewayServer gatewayServer = new GatewayServer(new DateRangeEntryPoint());
    gatewayServer.start();
    System.out.println("Gateway Server Started");

}

}

但是,当我在 Eclipse 中尝试 运行 时,出现以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    GatewayServer cannot be resolved to a type
    GatewayServer cannot be resolved to a type
    The constructor DateRangeEntryPoint() is undefined at statresearch.programs.DaypartParser.DateRangeEntryPoint.main(DateRangeEntryPoint.java:22)

我坚持的是如何在 Eclipse 中导入 py4j,以便我可以在 Python 中利用 Eclipse 项目中已经定义的对象。

您需要在项目的构建路径中包含 py4j JAR。最简单的路线可能是:

  1. 在您的 Eclipse 项目中创建一个 lib 文件夹(如果它尚不存在)。
  2. py4j0.x.jar 从 p4yj 安装复制到 lib 文件夹中。
  3. 在 Eclipse Package Explorer(或 Project Explorer)中右键单击 JAR,select Build Path > Add to Build Path.

此时您可以查看 Eclipse 的“问题”或“标记”视图以查看编译问题是否已消失。当您再次 运行 程序时,它应该会通过 "Unresolved compilation..." 错误。