2

I've read the first part of the py4j.org introduction, then I skipped down to the Eclipse section. I installed the Eclipse plugins found here: http://eclipse.py4j.org/ and restarted Eclipse afterwards.

I have a class in the pre-existing Java project known as DateRange, so I created a new class called DateRangeEntryPoint, per the instructions. This consisted of the following code.

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");

}

}

But, when I try to run this in eclipse, I get the following error:

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)

What I am stuck on is how to import py4j in Eclipse so that I can utilize, in Python, the objects already defined in a Eclipse project.

abhi
  • 1,760
  • 1
  • 24
  • 40
Kyle N Payne
  • 79
  • 11
  • 1
    Maybe you need the py4j dependency on your classpath - http://mvnrepository.com/artifact/net.sf.py4j/py4j/0.8.1 – Dan W Jun 02 '16 at 15:31
  • 1
    The eclipse.py4j.org is a repository of OSGi bundles/Eclipse plug-ins. It only makes sense to use them if you are developing a plug-in yourself. If it's a regular Java project, the Py4J eclipse plugins won't help. – Barthelemy Jun 03 '16 at 10:09
  • 1
    Thanks for your question though, I realize that I'll have to make this a lot clearer in the documentation for newcomers. – Barthelemy Jun 03 '16 at 10:18
  • No worries, I'm an absolute beginner when it comes to Java. The method below worked and I'm off and running with accessing Java objects in my python package. Thanks for this awesome package! – Kyle N Payne Jun 03 '16 at 14:45

1 Answers1

2

You need to have the py4j JAR(s) on the project's build path. Easiest route is probably:

  1. Create a lib folder in your Eclipse project (if it doesn't already exist).
  2. Copy the py4j0.x.jar from the p4yj installation into that lib folder.
  3. Right-click JAR in Eclipse Package Explorer (or Project Explorer), select Build Path > Add to Build Path.

At that point you can look at Eclipse's Problems or Markers views to see that the compilation problems have disappeared. When you run the program again, it should get past the "Unresolved compilation..." error.

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • A follow up to this: Do you know how I would access a Eclipse project in python? I already have Java code in a project, and I need to be able to run those classes within a python script. – Kyle N Payne Jun 02 '16 at 18:11
  • yeah, I know, I was just hoping maybe you could solve all of my problems :). Thanks! – Kyle N Payne Jun 02 '16 at 18:42