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 0.5.4.2). I will use a virtual environment here, so the syntax is simply:

cd /home/user/Downloads # path where the zip file was downloaded
unzip JPype-0.5.4.2.zip
cd JPype-0.5.4.2
python setup.py 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
<pre>
 
I got this answer: /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/include/jni.h telling me that my JAVA home is /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/
 
Then, edit the file setup.py, search for the function called setupLinux and update the following line 
 
<pre lang="python">
self.javaHome = '/usr/lib/jvm/java-1.5.0-sun-1.5.0.08' # Ubuntu linux
<pre>
 
with the proper JAVA path found abobe:
 
<pre lang="python">
self.javaHome = '/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/'

Try again the installation, which should work now

python setup.py 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 setup.py. In my case, I typed:

export JAVA_HOME="/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/"
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:

    http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

    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.