{"id":10026,"library":"pemja","title":"PemJa","description":"PemJa is a Python-Java bridge that enables embedding a JVM within a Python process, allowing dynamic interaction with Java objects and classes. It simplifies calling Java methods from Python and provides mechanisms for classpath management. The current version is 0.6.1, with releases occurring as features are added or bugs are fixed, but without a strict regular cadence.","status":"active","version":"0.6.1","language":"en","source_language":"en","source_url":"https://github.com/pemja/pemja","tags":["JVM","Java","bridge","interop","JNI","python-java"],"install":[{"cmd":"pip install pemja","lang":"bash","label":"Install PemJa"}],"dependencies":[],"imports":[{"symbol":"launch_jvm","correct":"from pemja import launch_jvm"},{"symbol":"find_jar","correct":"from pemja import find_jar"},{"note":"Used for type hinting Java objects returned by PemJa.","symbol":"JavaObject","correct":"from pemja import JavaObject"}],"quickstart":{"code":"import pemja\n\n# Ensure JDK 8+ is installed and accessible in your system's PATH or JAVA_HOME.\n# In a production environment, consider explicit JVM options or classpath management.\n\ntry:\n    print(\"Launching JVM...\")\n    # Using a context manager ensures the JVM is shut down cleanly\n    with pemja.launch_jvm() as jvm:\n        print(\"JVM launched successfully.\")\n\n        # 1. Access a static method from a core Java class\n        System = jvm.find_class(\"java.lang.System\")\n        java_version = System.getProperty(\"java.version\")\n        print(f\"Java version reported by JVM: {java_version}\")\n\n        # 2. Create an instance of a Java class and call a method\n        String = jvm.find_class(\"java.lang.String\")\n        greeting = jvm.create_instance(String, \"Hello from PemJa in Python!\")\n        print(f\"Original Java string: {greeting}\")\n        print(f\"Uppercase Java string: {greeting.toUpperCase()}\")\n\n        # 3. Handle exceptions from Java\n        # This is a simple example; real error handling would be more robust\n        try:\n            Integer = jvm.find_class(\"java.lang.Integer\")\n            Integer.parseInt(\"not_a_number\") # This will throw a Java NumberFormatException\n        except jvm.JavaException as e:\n            print(f\"Caught Java Exception: {e.get_stack_trace()}\")\n\nexcept pemja.PemjaException as e:\n    print(f\"PemJa specific error: {e}\")\n    print(\"Please ensure JDK 8+ is correctly installed and configured.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates launching a JVM, accessing static Java methods, creating Java object instances, calling instance methods, and basic Java exception handling. It's crucial to have JDK 8+ installed on your system for PemJa to function."},"warnings":[{"fix":"Review the PemJa documentation for `launch_jvm` (e.g., on GitHub) and update your calls to use the new explicit parameters. For general JVM arguments, use the `options` list (e.g., `options=['-Xmx512m']`).","message":"The parameters for `pemja.launch_jvm()` changed significantly in version 0.6.0. The `config` dictionary was replaced with more explicit parameters like `options` and `classpath`.","severity":"breaking","affected_versions":"0.6.0+"},{"fix":"Install JDK 8+ and ensure its `bin` directory is in your system's PATH environment variable, or set the `JAVA_HOME` environment variable to point to your JDK installation directory.","message":"PemJa requires a Java Development Kit (JDK) version 8 or higher to be installed on the system where Python is running. Without a properly configured JDK, PemJa cannot launch the JVM.","severity":"gotcha","affected_versions":"All"},{"fix":"Call `pemja.launch_jvm()` once at the beginning of your application. Store the returned `jvm` object and reuse it throughout your application. Use a context manager (`with pemja.launch_jvm() as jvm:`) for safe shutdown.","message":"The JVM should generally be launched only once per Python process using `pemja.launch_jvm()`. Attempting to launch it multiple times can lead to errors or undefined behavior.","severity":"gotcha","affected_versions":"All"},{"fix":"Use `pemja.find_jar()` to locate JARs and pass the result to the `classpath` argument of `pemja.launch_jvm()`. For multiple JARs, `classpath` accepts a list of paths.","message":"When working with custom Java JARs, ensuring they are correctly added to the JVM's classpath is critical. Incorrect classpath configuration leads to `NoClassDefFoundError` or similar issues.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure that the JAR file containing 'Your/Custom/Class' (and any of its dependencies) is correctly specified in the `classpath` argument when calling `pemja.launch_jvm()`. Use `pemja.find_jar()` to help locate JARs if they are in standard locations.","cause":"The specified Java class or one of its dependencies could not be found by the JVM. This usually means the JAR containing the class was not added to the JVM's classpath.","error":"pemja.JavaException: java.lang.NoClassDefFoundError: Your/Custom/Class"},{"fix":"Install JDK 8 or higher on your system. Then, either add the JDK's `bin` directory to your system's PATH environment variable, or set the `JAVA_HOME` environment variable to point to your JDK installation directory (e.g., `/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home` on macOS).","cause":"PemJa could not locate a Java Development Kit (JDK) installation on your system, which is required to launch the JVM.","error":"FileNotFoundError: Cannot find JDK home directory. Please ensure JAVA_HOME is set or java is in PATH."},{"fix":"Ensure `pemja.launch_jvm()` is called only once. Store the returned `jvm` object and reuse it throughout your application. If you need to reconfigure the JVM, you must restart your Python process.","cause":"You are attempting to call `pemja.launch_jvm()` more than once within the same Python process.","error":"pemja.PemjaException: JVM already launched. Can't launch it again."}]}