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

  1. Thanks a lot. This helped.

  2. rosni says:

    Thank you very much..this article helped me a lot!!

  3. Anonymous says:

    Thanks 😀

  4. Elliott says:

    To do this on OpenSUSE, I had to install the Oracle RPM from here:

    Then I found the java directory in /usr/java/jdk*/

    then i had to go into that directory and copy jni.h and jni_md.h into the Jpype installer directory JPype*/src/native/common/include

    Then it installed and worked!

Leave a Reply

Your email address will not be published.