Glad to see that you're working with object factories for Jython.  This is a topic that we hope to clarify a bit more by providing as much documentation as possible.  Indeed, object factories are a great way to access Jython classes from within Java code.

You should only have to include jython.jar in your classpath when making use of object factories.  I'm not sure why your code is not compiling, sorry to hear that.  It looks like you referenced that you're using 2.5b.  You should download 2.5.0 final as it has been released.  Also, are you using an IDE for working with Jython?  If not, I recommend using Netbeans 6.7 RC3 as it has great Jython/Python support and it may help you out with classpath issues (via error flagging on invalid imports and such). 

As I said previously, we are hoping to document object factories more soon.  There are many ways to achieve the end result of using Jython classes in Java.  One such solution is to use a factory like the one Charlie wrote about in the referenced Jython Monthly article.  Another way is to create a more loosely coupled object factory that has no hard coded object names.  This would allow you to access any Jython module by using just the one factory.  One such solution would be to create a singleton factory containing a method similar to the following:

public static Object createObject(Object interfaceType, String moduleName) {
        Object javaObj = null;
        PythonInterpreter interpreter = new PythonInterpreter();

        try {
            try {
            interpreter.exec("from " + moduleName + " import " + moduleName);
            pyObject = interpreter.get(moduleName);

            } catch (Exception ie) {
                // Do something with the exception
        } catch (ModuleNotFoundException ex) {
            Logger.getLogger(JythonObjectFactory.class.getName()).log(Level.SEVERE, null, ex);

        try {

            PyObject newObj = pyObject.__call__();

            javaObj = newObj.__tojava__(Class.forName(interfaceType.toString().substring(
                    interfaceType.toString().indexOf(" ") + 1, interfaceType.toString().length())));
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(JythonObjectFactory.class.getName()).log(Level.SEVERE, null, ex);

        return javaObj;

Of course, you'll want to implement better logging than in my example. 

After you code your factory, you could make use of it by implementing a Java interface in your Jython class, and then calling it from Java as shown below.  In this case we are coercing a Jython class that represents an automobile object.

AutomobileType automobile = (AutomobileType) factory.createObject(
                AutomobileType.class, "Automobile");
        // Do something with automobile now

AutomobileType is your Java interface, and Automobile is the name of the Jython class.  This should work with any Jython class...just implement a Java interface in your class and you can instantiate it via the factory.

Another similar solution that achieves better performance is to not use PythonInterpreter at all, but instead make use of PySystemState.  Jim Baker showed me how to make this type of factory work, and you can see one such implementation documented in the source repository for the upcoming Jython Book:

I hope this helps you out a bit.  Also, please stay tuned because the documentation is going to get better soon.


Josh Juneau
Twitter ID:  javajuneau

On Wed, Jun 24, 2009 at 4:28 AM, Sarah Mount <> wrote:
Hi everyone,

sorry to ask such a daft question, but I'm rather struggling to follow
the documentation on using Jython classes in Java. Jythonc has gone
from 2.5, so I'm assuming that the "approved" way to access Jython
classes is now the method described here:

and in the LearningJython wiki page -- is that right, or should we be
using the @JavaMethod annotations and so on?

I've written out the classes described in Charlie Groves tutorial, but
they currently don't compile because Javac can't find org.python.core.
I put jython.jar on my CLASSPATH, are there other Jars I should have

This is on Ubuntu Jackalope with Sun's 1.6 JRE and Jython 2.5bif it helps.

Many thanks,


Sarah Mount, Senior Lecturer, University of Wolverhampton

This email, together with any attachment, is for the exclusive and
confidential use of the addressee(s) and may contain legally
privileged information.  Any use, disclosure or reproduction without
the sender's explicit consent is unauthorised and may be unlawful.

Any e-mail including its content and any attachments may be monitored
and used by The University of Wolverhampton for reasons of security
and for monitoring internal compliance with the University's policy on
internet use. E-mail blocking software may also be used.  The
University cannot guarantee that this message or any attachment is
virus free or has not been intercepted and amended.

If you believe you have received this message in error please notify
the sender by email, telephone or fax and destroy the message and any

Jython-users mailing list