views:

582

answers:

2

My first attempt at jython is a java/jython project I'm writing in eclipse with pydev.

I created a java project and then made it a pydev project by the RightClick project >> pydev >> set as... you get the idea. I then added two source folders, one for java and one for jython, and each source folder has a package. And I set each folder as a buildpath for the project. I guess I'm letting you know all this so hopefully you can tell me wether or not I set the project up correctly.

But the real question is: how do I get my jython code made into a class file so the java code can use it? The preferred method would be that eclipse/pydev would do this for me automatically, but I can't figure it out. Something mentioned in the jython users guide implies that it's possible but I can't find info on it anywhere.

EDIT: I did find some information here and here, but things are not going too smooth.

I've been following the guide in the second link pretty closely but I can't figure out how to get jythonc to make a constructor for my python class.

A: 

Following the "Accessing Jython from Java Without Using jythonc" tutorial it became possible to use the jython modules inside java code. The only tricky point is that the *.py modules do not get compiled to *.class files. So it turns out to be exotic scripting inside java. The performance may of course degrade vs jythonc'ed py modules, but as I got from the jython community pages they are not going to support jythonc (and in fact have already dropped it in jython2.5.1).

So if you decide to follow non-jythonc approach, the above tutorial is perfect. I had to modify the JythonFactory.java code a bit:

String objectDef = "=" + javaClassName + "(your_constructor_params here)";
try {
       Class JavaInterface = Class.forName(interfaceName);

       System.out.println("JavaInterface=" + JavaInterface);

       javaInt = 
            interpreter.get("instance name of a jython class from jython main function").__tojava__(JavaInterface);
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();  // Add logging here
    }
D_K
+1  A: 

Jythonc doesn't exist anymore, it has been forked off to another project called Clamp, but with that said...

...you can pre-compile your python scripts to .class files using:

jython [jython home]/Lib/compileall.py [the directory where you keep your python code]

Source - Jython Newsletter, March 2009

When I fed it a folder with Python 2.7 code (knowing it would fail in Jython 2.5) it did output a .class file, even though it doesn't function. Try that with your Jython scripts. If it works, please let us know, because I'll be where you are soon enough.

Once you're that far, it isn't hard to convert your command line statement to an External Tool in PyDev that can be called as needed.

E-man