Access Java classes from Python
Go to file
Peter Badida b1ef7af97d
Merge pull request #395 from kivy/fix_constructor_exception
Always check for exception JavaClass.call_constructor()
2019-02-01 15:36:09 +01:00
docs Add warning about Java class comparing (.hashCode()) 2019-02-01 13:37:57 +01:00
examples/arraylists Add example of nested arrays and Python tuple/list as func arg 2019-01-03 00:06:41 +01:00
jnius Always check for exception JavaClass.call_constructor() 2019-02-01 12:44:38 +01:00
tests Add test for Java constructor 2019-02-01 14:05:45 +01:00
.appveyor.yml Add appveyor CI 2018-12-01 19:29:19 +01:00
.gitignore New line 2018-12-01 19:29:33 +01:00
.travis.yml Fix deployment due to broken matrix 2018-12-05 00:01:03 +01:00
CHANGELOG.md update changelog 2017-03-24 01:39:33 +01:00
LICENSE update copyright year 2017-03-23 19:20:24 +02:00
MANIFEST.in collect license file in manifest 2017-03-23 20:21:47 +02:00
Makefile remove a few end of line spaces 2018-04-30 14:34:58 +02:00
README.md replace "java" import with jnius 2018-04-30 13:54:43 +02:00
build.xml cleanup the build process. Use ant, put the java code in a proper java root. 2015-05-03 11:24:11 -04:00
jnius_config.py Ensure jnius_config.set_options and jnius_config.set_classpath set options as list types. 2017-12-19 12:58:38 -05:00
setup.py Use PY2 to separate CPython 2/3 for compat 2019-01-31 18:02:00 +01:00
setup_sdist.py Add separate setup file for source distribution 2018-10-19 22:36:48 +02:00

README.md

PyJNIus

A Python module to access Java classes as Python classes using JNI. Warning: the pypi name is now pyjnius instead of jnius.

Build Status PyPI

Installation

Requirements: Cython

pip install Cython
pip install pyjnius

Quick overview

>>> from jnius import autoclass
>>> autoclass('java.lang.System').out.println('Hello world')
Hello world

>>> Stack = autoclass('java.util.Stack')
>>> stack = Stack()
>>> stack.push('hello')
>>> stack.push('world')
>>> print stack.pop()
world
>>> print stack.pop()
hello

Usage on desktop

You need a java JDK installed (OpenJDK will do), Cython and make to build it. Please ensure that your JDK_HOME or JAVA_HOME environment variable points to the installed JDK root directory, and that the JVM library (jvm.so or jvm.dll) is available from your PATH environment variable. Failure to do so may result in a failed install, or a successful install but inability to use the pyjnius library.

make

That's it! You can run the tests using

make tests

to ensure everything is running correctly.

Usage with python-for-android

from time import sleep
from jnius import autoclass

Hardware = autoclass('org.renpy.android.Hardware')
print 'DPI is', Hardware.getDPI()

Hardware.accelerometerEnable(True)
for x in xrange(20):
    print Hardware.accelerometerReading()
    sleep(.1)

It will output something like:

I/python  ( 5983): Android kivy bootstrap done. __name__ is __main__
I/python  ( 5983): Run user program, change dir and execute main.py
I/python  ( 5983): DPI is 160
I/python  ( 5983): [0.0, 0.0, 0.0]
I/python  ( 5983): [-0.0095768067985773087, 9.3852710723876953, 2.2218191623687744]
I/python  ( 5983): [-0.0095768067985773087, 9.3948478698730469, 2.2218191623687744]
I/python  ( 5983): [-0.0095768067985773087, 9.3948478698730469, 2.2026655673980713]
I/python  ( 5983): [-0.028730420395731926, 9.4044246673583984, 2.2122423648834229]
I/python  ( 5983): [-0.019153613597154617, 9.3852710723876953, 2.2026655673980713]
I/python  ( 5983): [-0.028730420395731926, 9.3852710723876953, 2.2122423648834229]
I/python  ( 5983): [-0.0095768067985773087, 9.3852710723876953, 2.1835119724273682]
I/python  ( 5983): [-0.0095768067985773087, 9.3756942749023438, 2.1835119724273682]
I/python  ( 5983): [0.019153613597154617, 9.3948478698730469, 2.2122423648834229]
I/python  ( 5983): [0.038307227194309235, 9.3852710723876953, 2.2218191623687744]
I/python  ( 5983): [-0.028730420395731926, 9.3948478698730469, 2.2026655673980713]
I/python  ( 5983): [-0.028730420395731926, 9.3852710723876953, 2.2122423648834229]
I/python  ( 5983): [-0.038307227194309235, 9.3756942749023438, 2.2026655673980713]
I/python  ( 5983): [0.3926490843296051, 9.3086557388305664, 1.3311761617660522]
I/python  ( 5983): [-0.10534487664699554, 9.4331550598144531, 2.1068975925445557]
I/python  ( 5983): [0.26815059781074524, 9.3469638824462891, 2.3463177680969238]
I/python  ( 5983): [-0.1149216815829277, 9.3852710723876953, 2.31758713722229]
I/python  ( 5983): [-0.038307227194309235, 9.41400146484375, 1.8674772977828979]
I/python  ( 5983): [0.13407529890537262, 9.4235782623291016, 2.2026655673980713]

Advanced example

When you use autoclass, it will discover all the methods and fields of the object and resolve them. For now, it is better to declare and use only what you need. The previous example can be done manually as follows:

from time import sleep
from jnius import MetaJavaClass, JavaClass, JavaMethod, JavaStaticMethod

class Hardware(JavaClass):
    __metaclass__ = MetaJavaClass
    __javaclass__ = 'org/renpy/android/Hardware'
    vibrate = JavaStaticMethod('(D)V')
    accelerometerEnable = JavaStaticMethod('(Z)V')
    accelerometerReading = JavaStaticMethod('()[F')
    getDPI = JavaStaticMethod('()I')
    
# use that new class!
print 'DPI is', Hardware.getDPI()

Hardware.accelerometerEnable()
for x in xrange(20):
    print Hardware.accelerometerReading()
    sleep(.1)

Support

If you need assistance, you can ask for help on our mailing list:

We also have an IRC channel:

  • Server : irc.freenode.net
  • Port : 6667, 6697 (SSL only)
  • Channel : #kivy

Contributing

We love pull requests and discussing novel ideas. Check out our contribution guide and feel free to improve PyJNIus.

The following mailing list and IRC channel are used exclusively for discussions about developing the Kivy framework and its sister projects:

IRC channel:

  • Server : irc.freenode.net
  • Port : 6667, 6697 (SSL only)
  • Channel : #kivy-dev

License

PyJNIus is released under the terms of the MIT License. Please refer to the LICENSE file.