2

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

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

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 项目中已经定义的对象。

4

1 回答 1

2

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

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

此时,您可以查看 Eclipse 的 Problems 或 Markers 视图,看看编译问题已经消失。当您再次运行该程序时,它应该会通过“未解决的编译...”错误。

于 2016-06-02T15:46:29.367 回答