我已经阅读了 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 项目中已定义的对象。
最佳答案
您需要在项目的构建路径上有 py4j JAR。最简单的路线可能是:
- 在 Eclipse 项目中创建一个
lib
文件夹(如果尚不存在)。 - 将 p4yj 安装中的
py4j0.x.jar
复制到该lib
文件夹中。 - 在 Eclipse Package Explorer(或 Project Explorer)中右键单击 JAR,选择构建路径> 添加到构建路径。
此时,您可以查看 Eclipse 的 Problems 或 Markers View ,发现编译问题已经消失。当您再次运行该程序时,它应该会出现“未解析的编译...”错误。
关于java - 使用 Eclipse 导入 py4j,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37596091/