Installing JPype to use Java from Python

JPype is a Python package that allows you to call Java from Python.

First, download the zip file (I got version I will use a virtual environment here, so the syntax is simply:

cd /home/user/Downloads # path where the zip file was downloaded
cd JPype-
python install

It may work out of the box, but this was not the case for me…I got a compilation error about a missing header (nji.h). In fact, the README-LINUX.txt gives you a hint for the solution. First, locate nji.h by typing this command in a shell:

locate nji.h
I got this answer: /usr/lib/jvm/java-1.6.0-openjdk- telling me that my JAVA home is /usr/lib/jvm/java-1.6.0-openjdk-
Then, edit the file, search for the function called setupLinux and update the following line 
<pre lang="python">
self.javaHome = '/usr/lib/jvm/java-1.5.0-sun-' # Ubuntu linux
with the proper JAVA path found abobe:
<pre lang="python">
self.javaHome = '/usr/lib/jvm/java-1.6.0-openjdk-'

Try again the installation, which should work now

python install

Now, time for testing, enter a python shell and type:

>>> from jpype import *
>>> startJVM(getDefaultJVMPath(), "-ea", "-Djava.class.path=/tmp/Jpype/sample")
>>> java.lang.System.out.println("Hello World!!")
Hello World!!
>>> shutdownJVM()

Note that you must set your JAVA_HOME environment variable before to reflect what you changed in the In my case, I typed:

export JAVA_HOME="/usr/lib/jvm/java-1.6.0-openjdk-"
Please follow and like us:
This entry was posted in Linux, Python and tagged , , , . Bookmark the permalink.

4 Responses to Installing JPype to use Java from Python

Leave a Reply

Your email address will not be published.