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:
4 Responses to Installing JPype to use Java from Python