JPype Setup on Linux
johannes.liegl During the last days I faced the problem to enable the calling of Java methods from Python code. I stumbled upon the JPype project on sourceforge that does exactly what I needed. Setting JPype up on Windows was done in a couple of hours but for Linux it lastet 2 days. So here are my hints for all of those who want to use JPype on Linux without spending to much time for the setup.
You need:
- python
- python development package (e.g. apt-get install python2.4-dev)
- c-compiler (e.g. gcc)
Change to /usr/local/src and check out the 5.1 version of the JPype code from the CVS repository.
“cvs -z3 -d:pserver:anonymous@jpype.cvs.sourceforge.net:/cvsroot/jpype co -P jpype” should do this for you.
If not try to login to the CVS server before. Try “cvs -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/jpype login”.
So, by now you should have the JPype code in /usr/local/src/jpype - change to that directory.
Now you have to edit the setup.py file. There should be an if statement “if sys.platform==”win32″. Go to the else
branch of the if-statement and change JAVA_HOME to your JDK. On my machine it was /usr/local/java/jdk1.6.0_04.
Compile the source from /usr/local/src/jpype with “python ./setup.py install” this should work without errors and automatically install the jpype module into your python directory. at my machine it was /usr/lib/python2.4/site-packages/jpype. now
change to that directory. Edit _linux.py and alter line 37 so that it returns the path to your libjvm.so file.
(e.g. /usr/local/jdk1.6.0_04/jre/lib/i386/client/libjvm.so).
The installation manual of JPype says that you should export the path to your libjvm.so with
“export LD_LIBRARY_PATH=/usr/local/jdk1.6.0_04/jre/lib/i386:/usr/local/jdk1.6.0_04/jre/lib/i386/client” for example but I didn’t need to do so.
Now JPype should be up and running. You can import it in your python files using “import jpype”. Here is a little
hello world code snippet:
from jpype import *
class PythonCallsJava:
def callJava(self):
try:
startJVM("/usr/local/jdk1.6.0_04/jre/lib/i386/client/libjvm.so", "-ea")
java.lang.System.out.println("Hello World!")
except:
print "error"
shutdownJVM()
if __name__ == "__main__":
javaCaller=PythonCallsJava()
javaCaller.callJava()
Some links:
http://www.thescripts.com/forum/thread477930.html _Linux.py
http://sourceforge.net/projects/jpype/
http://cents.cs.berkeley.edu/tinywiki/index.php/Tinyos_Installation_Log
