3

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?

Community
  • 1
  • 1
compski
  • 709
  • 3
  • 13
  • 28
  • You say that it works on the CLI; what did you do there? – OldTinfoil Sep 10 '14 at 14:32
  • On CMD prompt? .. I basically compiled the java file on cmd prompt (using javac Greeter.java). Then navigating to the same directory as the Greeter.class, I ran Jython (on cmd) .. importing and calling the Java methods went perfectly fine – compski Sep 10 '14 at 14:36
  • What command did you use? How was Jython installed? Was it the standalone Jython jar? Or have you installed Jython to your system? – OldTinfoil Sep 10 '14 at 14:47
  • @IntrepidBrit On CMD, I typed "javac Greeter.java". On the same directory, I typed "jython". Then I typed "import Greeter g = Greeter() g.greet("yours truly")" and it worked Im using Jython 2.5.3 - the installer version @ (http://www.jython.org/downloads.html) installed in my system – compski Sep 10 '14 at 15:03

1 Answers1

1

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).

  • +1 the key thing that solved it was 'C:\Users\compski\workspace\test\target\bin' (stupid target folder hid the bin) thank you! – compski Sep 12 '14 at 07:36