8

I come from a Java world and am totally new to Jython.

Is it possible to create a project in Eclipse with both Jython and Java classes on the same project? I tried to do so -writing a simple Java class and using it in Jython module- and everything went fine during coding. But when I try to run the project I get:

Traceback (most recent call last):
File "/home/bahman/Work/Jython/TestJython/src/com/bahmanm/Main.py", line 1, in <module>
from com.bahmanm import Greeter
ImportError: cannot import name Greeter

The Java class is: package com.bahmanm;

public class Greeter {

 private String msg;

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

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

}

And the Jython module is quite simple:

from com.bahmanm import Greeter
g = Greeter()
g.greet("Bahman")

I'd appreciate any ideas/hints.

Tobias Kienzler
  • 25,759
  • 22
  • 127
  • 221
BahmanM
  • 1,420
  • 10
  • 18

3 Answers3

7

Add your java-code to the Pythonpath of your jython-project

Project

Properties -> PyDev PYTHONPATH -> External Libraries

Blauohr
  • 5,985
  • 2
  • 25
  • 31
  • 1
    Is there any way to use relative paths with these External Libraries definitions? I had a similar problem to BahmanM, but since my java and python classes were in different projects, I had to use a path all the way from root, which doesn't make it very portable (i.e. using /project/trunk/javaproject/src would work fine from /project/trunk/jythonproject/scripts, but it would fail when checked out as /project/branches/mybranch/javaproject/src). I've tried looking for a variable that defines the workspace root, but so far the documentation has stumped me. – Mark Booth Nov 04 '10 at 20:33
  • This doesn't appear to work. I have a simple .class file under a package root (i.e. in a directory called "root"). Whatever directory I try to include in my PythonPath for the project, I keep getting "unresolved import". When you say "add your java-code"... does this in fact mean a .jar rather than a directory? – mike rodent Feb 23 '16 at 20:43
0

try adding

import java

at the top

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
  • As far as I can see, this makes no difference unless you want to actually call java.something from the Jython script. – Mark Booth Nov 04 '10 at 20:23
-1

when u run it, u add jars to jython's build path with:

jython -Dpython.path=[jarname] [pyfilename]

codedude
  • 71
  • 1