Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyDev: ImportError when trying to call user-defined Java Method using Jython in Eclipse

My question is similar to ImportErrorCmd but I'm using Windows and trying to run this DAMN thing in PyDev Eclipse. I know how to get it to work on cmd but not in Eclipse.

QUESTION

Anyway, so here I am trying to get this example @ ImportErrorSimpleExample to work (the solution there didn't work for me). In Eclipse, I made a Java project with this code: (link -> C:\Users\compski\workspace\test\src\test\Greeter.java)

package test;

public class Greeter {

private String msg;

  public Greeter() {
     msg = "Hello, ";
  }

  public void greet(String name) {
     System.out.println(msg + name);
  }

}

In Eclipse also, I made a PyDev Project with the Jython code called me.py: (link -> C:\Users\compski\workspace\Jython\Test\me.py)

from test import Greeter

g = Greeter()
g.greet("yours truly")

Attempts to fix "ImportError" but failed:

1) I tried adding my java code to PYTHONPATH (C:\Users\compski\workspace\test\src\test\Greeter.java and C:\Users\compski\workspace\test\src\test) as in here -> Proposed Solution 1 . Still didn't work

2) I set my Java project to PyDev project (rightclick Java project -> PyDev -> Set as Pydev Project but I don't have any "bin folders". I then also project referenced my Java Project containing the Greeter.java to my PyDev project as in here -> Proposed solution 2. Still didn't work

3) 1 guy from the chat at SO told me that I need to "you need to add directories, jars from which java will look for classes that is you give it c:\foo\bar\bazand import zyxxy.Frobnicator then it looks for c:\foo\bar\baz\xyzzy\Frobnicator" but I don't think I fully understand what he meant cause it sounds like what I did at 1)

4) ......Your answer?

like image 382
compski Avatar asked May 09 '26 09:05

compski


1 Answers

Ok, now that we have real file names here: given the PYTHONPATH of C:\Users\compski\workspace\test\src\test, after you do from test import Greeter, it will try to find the Greeter.class in the each entry of the PYTHONPATH; that is it tries to find C:\Users\compski\workspace\test\src\test\test\Greeter.class and C:\Users\compski\workspace\test\src\test\Greeter.java\test\Greeter.class, neither of which exists.

Instead, in Eclipse if I remember correctly you can add a project to the PYTHONPATH, this could be preferred for testing in the IDE - thus just add your test project as such in the PYTHONPATH. Another option is to add the directory from which test\Greeter.class is found; that is in Eclipse this should be C:\Users\compski\workspace\test\bin - that is by default the Java-natured projects compile files from src to the (hidden) bin folder within the project.

Finally if the error is about GreeterClass not found in test, do notice that test is a builtin python module name (though IIRC Jython does not have a module by that name).

like image 157