Tuesday, October 27, 2015

Python Access Java Through JNIus


Can python access java library or class?, yes it's possible. There are some project to do this, the popular one is PyJNIus and this project under Kyvi project https://github.com/kivy/pyjnius.

How it's work? PyJnius wrap java using JNI call so python can call it, don't worry if you don't know about it. Me to :-)

To install PyJNIus, we need to install
  1.  Java JDK installed (OpenJDK will do)
  2.  Python 3.4 (In this case I use 3.4 version)
  3.  Setup JAVA_HOME environment
  4.  Setup JRE_HOME environment

After install python, we need to install cython, use pip or easy install. In this case I use pip.
$ sudo pip install cython
After cython installed, then install PyJNIus through  php or easy install. In this case I use pip
$ sudo pip install PyJnius
After PyJnius installed then try import jnius from python, and let learn little thing about calling java from python.

On linux should be no problem when importing jnius,
Python 3.4.3 (default, Mar 10 2015, 14:53:35)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from jnius import autoclass
>>> 
But on OSX we sometime meet this error when import jnius
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/barry/anaconda/envs/ovation-testing/lib/python2.7/site-packages/ovation/__init__.py", line 8, in <module>
    from jnius import autoclass, cast, JavaException
  File "/Users/barry/anaconda/envs/ovation-testing/lib/python2.7/site-packages/jnius/__init__.py", line 12, in <module>
    from .jnius import *
ImportError: dlopen(/Users/barry/anaconda/envs/ovation-testing/lib/python2.7/site-packages/jnius/jnius.so, 2): Library not loaded: @rpath/libjvm.dylib
  Referenced from: /Users/barry/anaconda/envs/ovation-testing/lib/python2.7/site-packages/jnius/jnius.so
  Reason: image not found
If you found that problem, you need to add this to .profile in your home file, and add this line then restart your terminal then try again to import jnius.
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$(/usr/libexec/java_home)/jre/lib/server
now you can import jnius on python interpreter.

On windows, I have no luck to install PyJnius through pip or easy install using MinGW as compiler. But I still struggling with it :-(.

now let's test to access some java library
Python 3.4.3 (default, Mar 10 2015, 14:53:35)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import jnius
>>> from jnius import autoclass
>>> Stack = autoclass('java.util.Stack')
>>> stack = Stack()
>>> stack.push('hello')
'hello'
>>> stack.push('world')
'world'
>>> stack.pop()
'world'
>>> stack.pop()
'hello'
>>> System = autoclass('java.lang.System')
>>> System.out.println('Hello World')
Hello World
for more learning stuff you can refer to:
http://pyjnius.readthedocs.org/en/latest/
http://kivy.org/planet/2012/08/pyjnius-accessing-java-classes-from-python/

with this project, it's possible to develop android app using python. To create android package we can use http://python-for-android.readthedocs.org/en/latest/

No comments:

Post a Comment