Access Java classes from Python
Go to file
Mirko Galimberti 17563c6f7a
Use specified `architecture` for CI tests instead of guessing it on `setup-python` (#725)
2024-05-29 22:37:24 +02:00
.github/workflows Use specified `architecture` for CI tests instead of guessing it on `setup-python` (#725) 2024-05-29 22:37:24 +02:00
docs Update dependency furo to v2024 (#724) 2024-05-29 18:55:00 +02:00
examples/arraylists Add example of nested arrays and Python tuple/list as func arg 2019-01-03 00:06:41 +01:00
jnius Make doc structure consistent and up-to-date (#703) 2023-12-03 16:41:40 +01:00
tests introduce protocol_map for Map$Entry (#659) 2023-09-10 13:50:47 +02:00
.gitignore New line 2018-12-01 19:29:33 +01:00
.readthedocs.yaml Add (now mandatory) `.readthedocs.yaml` file, add docs `requirements.txt` and update sphinx conf (#684) 2023-10-10 08:50:53 +02:00
CHANGELOG.md Update CHANGELOG and version for release `1.6.1` (#694) 2023-11-05 16:37:43 +01:00
CODE_OF_CONDUCT.md Make doc structure consistent and up-to-date (#703) 2023-12-03 16:41:40 +01:00
CONTACT.md Make doc structure consistent and up-to-date (#703) 2023-12-03 16:41:40 +01:00
CONTRIBUTING.md Make doc structure consistent and up-to-date (#703) 2023-12-03 16:41:40 +01:00
FAQ.md Make doc structure consistent and up-to-date (#703) 2023-12-03 16:41:40 +01:00
LICENSE Make doc structure consistent and up-to-date (#703) 2023-12-03 16:41:40 +01:00
MANIFEST.in collect license file in manifest 2017-03-23 20:21:47 +02:00
Makefile switch from nose to pytest 2019-12-06 00:39:14 +01:00
README.md Make doc structure consistent and up-to-date (#703) 2023-12-03 16:41:40 +01:00
build.xml Support Java 20, remove Java 7 support (#672) 2023-09-23 18:01:27 +02:00
jnius_config.py Remove pkg_resources for Python >=3.9 (#673) 2023-10-01 15:23:04 +02:00
main.workflow actions 2019-09-17 14:49:17 +02:00
pyproject.toml Support both Cython >3 and Cython < 3 (#669) 2023-09-16 13:57:15 +02:00
renovate.json Add renovate.json (#715) 2024-05-26 18:31:41 +02:00
setup.cfg Remove Python 2 support, six dependency (#619) 2022-10-16 14:56:56 +02:00
setup.py Make doc structure consistent and up-to-date (#703) 2023-12-03 16:41:40 +01:00
setup_sdist.py Remove support for Python 3.7 as reached EOL (#713) 2024-05-25 12:24:03 +02:00

README.md

PyJNIus

PyJNIus is a Python library for accessing Java classes using the Java Native Interface (JNI).

PyJNIus is managed by the Kivy Team and can be used with python-for-android.

It can also be used independently of Kivy, on desktop and mobile platforms.

[!WARNING] The PyPI package name is now pyjnius instead of jnius.

Backers on Open Collective Sponsors on Open Collective GitHub contributors Contributor Covenant

PyPI - Version PyPI - Python Version

Tests Tests (x86) Builds

Installation

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 with python-for-android

  • Get python-for-android
  • Compile a distribution with kivy (PyJNIus will be automatically added)

Then, you can do this kind of thing:

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 range(20):
    print(Hardware.accelerometerReading())
    sleep(0.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 class and resolve them. You may want 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 range(20):
    print(Hardware.accelerometerReading())
    sleep(0.1)

You can use the signatures method of JavaMethod and JavaMultipleMethod, to inspect the discovered signatures of a method of an object

>>> String = autoclass('java.lang.String')
>>> dir(String)
['CASE_INSENSITIVE_ORDER', '__class__', '_JavaClass__cls_storage', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__javaclass__', '__javaconstructor__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__pyx_vtable__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'charAt', 'checkBounds', 'clone', 'codePointAt', 'codePointBefore', 'codePointCount', 'compareTo', 'compareToIgnoreCase', 'concat', 'contains', 'contentEquals', 'copyValueOf', 'empty', 'endsWith', 'equals', 'equalsIgnoreCase', 'finalize', 'format', 'getBytes', 'getChars', 'getClass', 'hashCode', 'indexOf', 'indexOfSupplementary', 'intern', 'isEmpty', 'join', 'lastIndexOf', 'lastIndexOfSupplementary', 'length', 'matches', 'nonSyncContentEquals', 'notify', 'notifyAll', 'offsetByCodePoints', 'regionMatches', 'registerNatives', 'replace', 'replaceAll', 'replaceFirst', 'split', 'startsWith', 'subSequence', 'substring', 'toCharArray', 'toLowerCase', 'toString', 'toUpperCase', 'trim', 'valueOf', 'wait']
>>> String.format.signatures()
[(['java/util/Locale', 'java/lang/String', 'java/lang/Object...'], 'java/lang/String'), (['java/lang/String', 'java/lang/Object...'], 'java/lang/String')]

Each pair contains the list of accepted arguments types, and the returned type.

Troubleshooting

Make sure a Java Development Kit (JDK) is installed on your operating system if you want to use PyJNIus on desktop. OpenJDK is known to work, and the Oracle Java JDK should work as well.

On Windows, make sure JAVA_HOME points to your Java installation, so PyJNIus can locate the jvm.dll file allowing it to start Java. This shouldn't be necessary on macOS and Linux, but in case PyJNIus fails to find it, setting JAVA_HOME should help.

License

PyJNIus is MIT licensed, actively developed by a great community and is supported by many projects managed by the Kivy Organization.

Documentation

Documentation for this repository.

Support

Are you having trouble using PyJNIus or any of its related projects in the Kivy ecosystem? Is there an error you dont understand? Are you trying to figure out how to use it? We have volunteers who can help!

The best channels to contact us for support are listed in the latest Contact Us document.

Contributing

PyJNIus is part of the Kivy ecosystem - a large group of products used by many thousands of developers for free, but it is built entirely by the contributions of volunteers. We welcome (and rely on) users who want to give back to the community by contributing to the project.

Contributions can come in many forms. See the latest Contribution Guidelines for how you can help us.

Code of Conduct

In the interest of fostering an open and welcoming community, we as contributors and maintainers need to ensure participation in our project and our sister projects is a harassment-free and positive experience for everyone. It is vital that all interaction is conducted in a manner conveying respect, open-mindedness and gratitude.

Please consult the latest Code of Conduct.

Contributors

This project exists thanks to all the people who contribute. [Become a contributor].

Backers

Thank you to all of our backers! 🙏 [Become a backer]

Sponsors

Special thanks to all of our sponsors, past and present. Support this project by [becoming a sponsor].

Here are our top current sponsors. Please click through to see their websites, and support them as they support us.