Pyjnius
Pyjnius is a Python library for accessing Java classes and objects using the Java Native Interface (JNI). It can either start a new Java Virtual Machine (JVM) within the Python process or attach to an already existing one, making it suitable for both desktop and Android applications. Actively maintained by the Kivy team, it typically sees new releases every few months. The current stable version is 1.7.0.
Common errors
-
jnius.JavaException: Class not found 'org/package/MyClass'
cause The specified Java class or JAR file is not found in the JVM's classpath, or the class name is malformed (e.g., using '.' instead of '/' for nested classes, or including '.class' suffix).fixEnsure your Java class or JAR is correctly compiled and its path is added to Pyjnius's classpath using `jnius_config.add_classpath()` before `autoclass` is called. Use '/' for nested classes (e.g., `java.util.Map$Entry`). Do not append '.class' to the class name. -
java.lang.NoClassDefFoundError
cause Similar to 'Class not found', this usually means the JVM cannot locate the class definition. This can also happen if a class depends on another class that is missing, or if there's a Java version mismatch between compilation and runtime.fixVerify all required JARs are in the classpath. Check that the Java classes were compiled with a compatible Java version (e.g., compiling with Java 11 and running on JVM 8 can cause issues). For Android, ensure `pyjnius` is in your `buildozer.spec` requirements. -
ImportError: DLL load failed: The specified module could not be found.
cause This error, common on Windows, indicates that Pyjnius cannot find the Java Virtual Machine's dynamic link library (`jvm.dll`). This is usually due to `JAVA_HOME` being unset or incorrectly configured.fixSet the `JAVA_HOME` environment variable to your JDK installation directory (e.g., `C:\Program Files\Java\jdk-17`). Ensure the `%JAVA_HOME%\jre\bin\server` (for older JDKs) or equivalent path containing `jvm.dll` is in your system's `PATH` environment variable. -
error: subprocess-exited-with-error (during pip install)
cause This typically occurs when pip attempts to build Pyjnius from source because a pre-built wheel is unavailable, and required build tools (like a JDK or Cython) are missing from the system or not properly configured.fixEnsure a JDK is installed and `JAVA_HOME` is set. Also, ensure Cython is installed (`pip install Cython`). If specific Python versions or architectures lack wheels, you might need to resolve system-level build dependencies like GCC on Linux.
Warnings
- breaking Python 3.7 is no longer officially supported by Pyjnius 1.7.0. Version 1.6.0 was the last to likely support it. Using Pyjnius with Python 3.7 or older may lead to build or runtime errors.
- breaking Java Development Kit (JDK) is a mandatory prerequisite. Pyjnius needs a JDK installed on your system to compile and run. The `JAVA_HOME` environment variable must be correctly set, especially on Windows, so Pyjnius can locate the `jvm.dll` or `libjvm.so`.
- gotcha Pyjnius 1.6.0 removed support for Java 7 and added support for Java 20. Ensure your Java code (e.g., `.jar` files) is compiled with a Java version compatible with the JVM Pyjnius uses at runtime.
- deprecated Pyjnius 1.6.0 was likely the last release to ship with 32-bit pre-built wheels for Windows. Future versions may require manual compilation or only offer 64-bit wheels.
- gotcha Building Pyjnius from source (e.g., if no pre-built wheel is available for your platform/Python version) requires Cython. Older Pyjnius versions might have compilation issues with Cython 3.x.
Install
-
pip install pyjnius
Imports
- autoclass
import jnius; jnius.autoclass(...)
from jnius import autoclass
- JavaClass, MetaJavaClass, JavaMethod, PythonJavaClass
from jnius import JavaClass, MetaJavaClass, JavaMethod
Quickstart
from jnius import autoclass
# Access a standard Java class
Stack = autoclass('java.util.Stack')
stack = Stack()
stack.push('hello')
stack.push('world')
print(f"Popped: {stack.pop()}")
print(f"Popped: {stack.pop()}")
# Access static methods and fields
System = autoclass('java.lang.System')
System.out.println('Hello from Pyjnius!')