Python and Java with Jep – Getting Started

There may be a time when you need to interface java with Python scripts. Recently I needed to use SciPy to make use of its excellent numerical processing capabilities, yet most of my data and inputs were being created from a Java based application.

SciPy is a mature stable numerical and scientific library that allows users to simply and quickly try different processing techniques on their data.

In my case I needed to compare dynamically generated images for similarities. From the literature, I found that SciPy provided excellent support for Fourier Transforms and correlation of data. How to pass data to Python from Java and get the data back into Java though?

this is where Jepp comes in. Its sourceforge page is here:

So first things first, you’ll need to install Python (version 2.5). you can get that (in a ready made installer) from here:

Simply download and run the installer, and you should get a c:\Python25 directory with all the goodies.

Now download and install Jepp (note that the Jepp installer is tied to the Python version, so Jepp for Python 2.6 will not work with Python 2.5). After installing Jepp, you should get a Jepp folder under your Program Files directory.

You’ll notice that under the Jepp folder, there’s a JAR file called “jep.jar”. This is the library we will need to import into our Java Program in order to be able to use the Jep provided Java objects to load the libraries.

Jepp will take care of all the loading of the native libraries and interfaces for us, however we’ll need to tell it where everything lives.


1. Set LD_LIBRARY_PATH (create it if it does not exist) to the “C:\WINDOWS\System32\” folder. (you may want to add the current directory as well in case you are using your own native libararies). You should end up with teh LD_LIBRARY_PATH variable looking like this: “C:\WINDOWS\System32\;.”

2. Set the LD_PRELOAD environment variable (again, create it if it does not exist) to the location of the python25.dll (or the equivalent version eg python26.dll). This is usuall found in the system32 folder.

3. Add the location of the JVM client (jvm.dll) to the LD_PRELOAD path. This resides in the bin/client folder of your JRE location. Note that it needs to live there, and you can’t move it because it uses relative paths to other resources. Your LD_PRELOAD environment variable should look something like: “C:\WINDOWS\System32\python25.dll;c:\Program Files\Java\jre6\bin\client”

That’s it! We’re now ready to write a little program mixing Java and Python.

OK, start a new Netbeans project. In the libraries folder of the project, click “Add JAR/Folder”. Alternatively copy the jep.jar folder to somewhere on your classpath.

Now to write our little test program.

First create a small class:

public class Test
    String test;
   public void setTestString(String s)
      test = s;
   public String getTestString()
      return test;

Now in your main method :

Test testObject = new TestObject();
//To be able to call Python we need to have a Jep object:
Jep jep = new Jep();
//set a single variable so we can access it from Python
jep.set("mydata", "the data passed from Java");
//set our object that we'll put our data back into
jep.set("myObject", testObject);
//Now we run the script (this should be located in the current directory, //or you can use the full path to the script)
//Now print the data we got from the Script into our object
System.out.println("PYTHON SAID:" + testObject.getTestString());


In the previous section we needed to run a file called “”. Obviously now we need to make one…

Create a file called “”. Open it and enter the following :

print "Running Python Test..."
#here we print the variable we set in Java
#and here we set some data in the object we passed.
myObject.setTestString('Hello from Python')

That’s it! We’re all done, and we can run the little program. This was just a small example showing you how to get started, Jepp provides a lot more functionality (type conversion etc.) – you can read the forums and browse the source for more information on how to do specific things.