Your code certainly looks okay.  I do not use Eclipse, and I think that perhaps the issue has got something to do with the way in which the IDE is setting up your CLASSPATH.  You should ensure that you have the jython.jar in your CLASSPATH as well as all of the required Java sources.  I do not believe that your "sys.add_package('jyinterface.interfaces','jyinterface.factory', 'org.python.core')" line is should work without it.  I use Netbeans and I have to ensure that the directory containing my Jython sources is contained within my project CLASSPATH along with jython.jar and plyjy.jar if I am using it.  I do this by going into the project properties and manually adding each of these items as compile-time libraries.  You may be seeing similar requirements with Eclipse.

If you wish, download the PlyJy jar file and then put it into your project.  There should be no need to code a factory implementation if you use PlyJy...just use one of the included factory implementations.  You should then be able to use the factory by doing something like the following:

import org.plyjy.factory.PySystemObjectFactory;

 PySystemObjectFactory factory = new PySystemObjectFactory(
                EmployeeType.class, "employee", "EmployeeSep");

        EmployeeType employee = (EmployeeType) factory.createObject("Sandra", "Johnson",1);
        // use employee object

Hope that this works out for you.  Let me know if you have any questions with it.


Josh Juneau
Twitter ID:  javajuneau

On Tue, Jan 12, 2010 at 3:51 AM, Sandra Johnson <> wrote:
Thanks Josh. Much appreciated.

I will definitely look at PlyJy and the online jython book.
I have copied the factory code below as well as the python code. They are just the examples that you and Charlie posted in newsletters in 2006.

In Eclipse I have two projects. A PyDev project with and in it and then the java project with the java code for EmployeeFactory, EmployeeType, MainOct and MainSep.

I am also interested in the PyMC package for MCMC simulations. Would it be possible for me to write code that would port the functionality to Jython? I have written a Java gui and want to perform some MCMC simulations based on user information entered, store some results and then interface to a bayesian network. That is the plan.....!

package jyinterface.factory;

import jyinterface.interfaces.EmployeeType;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;

public class EmployeeFactory {
    private PyObject jyEmployeeClass;
    public EmployeeFactory () {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("from EmployeeOct import EmployeeOct");
        jyEmployeeClass = interpreter.get("EmployeeOct");
    public EmployeeType create(String first, String last, String id) {
        PyObject employeeObj = jyEmployeeClass.__call__(new PyString(first),
                                                        new PyString(last),
                                                        new PyString(id));
        return (EmployeeType)employeeObj.__tojava__(EmployeeType.class);


import sys
sys.add_package('jyinterface.interfaces','jyinterface.factory', 'org.python.core')

from jyinterface.interfaces import EmployeeType
class EmployeeOct(EmployeeType):
    def __init__(self, first, last, id):
        self.first = first
        self.last = last = id
    def getEmployeeFirst(self):
        return self.first
    def getEmployeeLast(self):
        return self.last
    def getEmployeeId(self):

Thanks again.


On Tue, Jan 12, 2010 at 2:41 PM, Josh Juneau <> wrote:
All you really need to ensure is that the Jython object that you wish to use from your java code is implementing a java interface.  The factory should then be able to coerce the jython object. If you could send your factory code to the list then I'd be more than happy to take a look at it.

On another note, you may be interested in taking a look at the PlyJy project which resides on Kenai. Jim Baker and myself have contributed a couple of different object factory implementations that you can use there. I also suggest taking a look at the online jython book located at We've written a chapter for that book which is all about using java and jython together.

Hope this helps. As I had mentioned, please feel free to send your code and I'd gladly have a look.


Josh Juneau
Application Developer

On Jan 11, 2010, at 5:30 PM, Sandra Johnson <> wrote:

Dear All,

I am new to Jython and decided to try the code supplied in the monthly Newsletters of September and October 2006 as I want to be able to write some object factories for Python classes.

I am coding and testing in Eclipse vsn 3.5.1 with PyDev plugin installed,  Jython 2.5.1 and JRE 6

I copied the code from the newsletters and changed the classes that differed between the two newsletters to have either Sep or Oct in them to distinguish where they came from. I have spent ages trying to find the problem and getting nowhere. One suggestion in an earlier post was to include into the python class

import sys
sys.add_package(' java package' ) - so I added this code as well for the java package 'jyinterface.interfaces'

but to no avail, same NullPointerException & ExceptionInInitializerError.

The only differences between my code and the code provided by Charlie Groves and Josh Juneau are java class name changes (, in package jyinterface.interfaces, and in the Jython project and I put not in package jyinterface as Josh had, but in the jyinterface.interfaces package). I also updated the references to these classes.

Here is the console output I get. Happy to supply the code if anyone feels kind enough to have a look!

Exception in thread "main" java.lang.ExceptionInInitializerError
   at org.python.core.PyObject.<clinit>(
   at org.python.util.PythonInterpreter.<init>(
   at org.python.util.PythonInterpreter.<init>(
   at jyinterface.factory.EmployeeFactory.<init>(
   at jyinterface.interfaces.MainOct.main(
Caused by: java.lang.NullPointerException
   at org.python.core.PyType.init(
   at org.python.core.PyType.createType(
   at org.python.core.PyType.addFromClass(
   at org.python.core.PyType.fromClass(
   at org.python.core.PyType.<clinit>(
   ... 5 more

The lines that are being referred to in the error log are
       EmployeeFactory factory = new EmployeeFactory();
       PythonInterpreter interpreter = new PythonInterpreter();

public PythonInterpreter() {    // line 59
       this(null, null);                   // line 60
           dict = new PyStringMap();
   public static final PyType TYPE = PyType.fromClass(PyObject.class);
           if (!Py.BOOTSTRAP_TYPES.contains(underlying_class)) {
       return createType(c, needsInners);
           addFromClass(PyType.class, null);
   public static PyType TYPE = fromClass(PyType.class);

This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
Jython-users mailing list

Dr Sandra Johnson
School of Mathematical Sciences
Room O311, O Block, Gardens Point
Queensland University of Technology
G.P.O. Box 2434
Brisbane, QLD 4001

t: +61 (0)7 3138 8314
f: +61 (0)7 3138 2310
m: 0416 371091