【问题标题】:Importing py4j using Eclipse使用 Eclipse 导入 py4j
【发布时间】:2016-06-02 15:26:58
【问题描述】:

我已经阅读了 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 依赖于你的类路径 - mvnrepository.com/artifact/net.sf.py4j/py4j/0.8.1
  • eclipse.py4j.org 是 OSGi 包/Eclipse 插件的存储库。只有在您自己开发插件时使用它们才有意义。如果它是一个常规的 Java 项目,Py4J eclipse 插件将无济于事。
  • 谢谢你的问题,我知道我必须在新人的文档中更清楚地说明这一点。
  • 别担心,我绝对是 Java 初学者。下面的方法有效,我正在关闭并运行我的 python 包中的 Java 对象。感谢这个很棒的包!

标签: java python eclipse py4j


【解决方案1】:

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

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

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

【讨论】:

  • 对此的跟进:你知道我将如何在 python 中访问 Eclipse 项目吗?我的项目中已经有 Java 代码,我需要能够在 python 脚本中运行这些类。
  • 是的,我知道,我只是希望你能解决我所有的问题 :)。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-26
  • 1970-01-01
  • 2015-02-15
  • 2012-01-02
  • 1970-01-01
  • 2011-01-21
  • 1970-01-01
相关资源
最近更新 更多