{"id":7575,"library":"pyjnius","title":"Pyjnius","description":"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.","status":"active","version":"1.7.0","language":"en","source_language":"en","source_url":"https://github.com/kivy/pyjnius","tags":["Java","JNI","Kivy","Android","JVM","reflection","interoperability"],"install":[{"cmd":"pip install pyjnius","lang":"bash","label":"Install latest stable version"}],"dependencies":[],"imports":[{"note":"While `import jnius` works, `from jnius import autoclass` is the canonical and clearer import for the primary function.","wrong":"import jnius; jnius.autoclass(...)","symbol":"autoclass","correct":"from jnius import autoclass"},{"note":"Used for advanced scenarios like subclassing Java classes in Python or implementing Java interfaces.","symbol":"JavaClass, MetaJavaClass, JavaMethod, PythonJavaClass","correct":"from jnius import JavaClass, MetaJavaClass, JavaMethod"}],"quickstart":{"code":"from jnius import autoclass\n\n# Access a standard Java class\nStack = autoclass('java.util.Stack')\nstack = Stack()\nstack.push('hello')\nstack.push('world')\n\nprint(f\"Popped: {stack.pop()}\")\nprint(f\"Popped: {stack.pop()}\")\n\n# Access static methods and fields\nSystem = autoclass('java.lang.System')\nSystem.out.println('Hello from Pyjnius!')\n","lang":"python","description":"This example demonstrates how to use `autoclass` to reflect and interact with standard Java classes like `java.util.Stack` and `java.lang.System`. It shows how to instantiate objects, call methods, and access static fields."},"warnings":[{"fix":"Upgrade your Python environment to 3.8 or newer. Python 3.12+ is fully supported.","message":"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.","severity":"breaking","affected_versions":"<= 1.6.x (Python 3.7)"},{"fix":"Install a compatible JDK (OpenJDK is recommended) and set the `JAVA_HOME` environment variable to its installation path. On Windows, ensure the JRE's `bin/server` directory containing `jvm.dll` is in your PATH.","message":"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`.","severity":"breaking","affected_versions":"All versions"},{"fix":"Use a compatible JDK for both Pyjnius and your Java artifacts. If you encounter 'Class not found' errors, check if your Java classes were compiled with a newer Java version than the JVM loaded by Pyjnius.","message":"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.","severity":"gotcha","affected_versions":">= 1.6.0"},{"fix":"Consider upgrading to a 64-bit Python environment on Windows if possible. If not, be prepared to build Pyjnius from source or check for community-maintained 32-bit wheels.","message":"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.","severity":"deprecated","affected_versions":">= 1.6.0 (32-bit Windows)"},{"fix":"Upgrade Pyjnius to 1.6.0 or newer, which supports both Cython < 3 and Cython >= 3. Alternatively, use an older Cython version (e.g., < 3) if stuck on an older Pyjnius.","message":"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.","severity":"gotcha","affected_versions":"< 1.6.0 (with Cython >= 3.0)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure 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.","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).","error":"jnius.JavaException: Class not found 'org/package/MyClass'"},{"fix":"Verify 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.","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.","error":"java.lang.NoClassDefFoundError"},{"fix":"Set 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.","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.","error":"ImportError: DLL load failed: The specified module could not be found."},{"fix":"Ensure 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.","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.","error":"error: subprocess-exited-with-error (during pip install)"}]}