{"library":"py4j","title":"Py4J: Python to Java Bridge","description":"Py4J enables Python programs to dynamically access Java objects in a Java Virtual Machine (JVM). It facilitates method calls and allows access to Java collections as if they were native Python objects. Py4J also supports callbacks, enabling Java programs to call Python objects. The current version is 0.10.9.9, and it maintains a consistent release cadence with a strong focus on backward compatibility.","language":"python","status":"active","last_verified":"Tue May 19","install":{"commands":["pip install py4j"],"cli":{"name":"py4j","version":"sh: 1: py4j: not found"}},"imports":["from py4j.java_gateway import JavaGateway","from py4j.java_gateway import GatewayParameters"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nfrom py4j.java_gateway import JavaGateway, GatewayParameters\nimport subprocess\nimport time\n\n# --- Java Gateway Server Setup (for demonstration) ---\n# In a real application, the Java Gateway Server would be started separately.\n# For this example, we'll try to start a minimal one programmatically.\n# This assumes 'py4j<version>.jar' is in the classpath. For simplicity, \n# we'll use a placeholder for py4j.jar path. Users need to replace it.\n# Usually, py4j.jar is found in your Python environment's site-packages/py4j/ directory.\n\n# NOTE: Replace '/path/to/py4j<version>.jar' with the actual path to your Py4J JAR file.\n# You can find it by running: `python -c \"import py4j; import os; print(os.path.join(os.path.dirname(py4j.__file__), 'py4j0.10.9.9.jar'))\"`\nPY4J_JAR_PATH = os.environ.get('PY4J_JAR_PATH', '/path/to/py4j0.10.9.9.jar') # Update this path!\n\njava_code = \"\"\"\nimport py4j.GatewayServer;\n\npublic class AdditionApplication {\n    public int addition(int first, int second) {\n        return first + second;\n    }\n\n    public static void main(String[] args) {\n        AdditionApplication app = new AdditionApplication();\n        GatewayServer server = new GatewayServer(app, 25333);\n        server.start();\n        System.out.println(\"Gateway Server Started on port 25333\");\n    }\n}\n\"\"\"\n\n# Attempt to compile and run the Java code for quickstart demo\n# This is a simplification and might require manual setup for complex environments.\n# In a production setting, the Java server would be a separate, long-running process.\n\n# Create temporary files for Java code and compiled class\njava_file_name = \"AdditionApplication.java\"\nclass_file_name = \"AdditionApplication.class\"\n\nwith open(java_file_name, \"w\") as f:\n    f.write(java_code)\n\ntry:\n    # Compile Java code (requires javac in PATH)\n    compile_process = subprocess.run(\n        [\"javac\", \"-cp\", PY4J_JAR_PATH, java_file_name],\n        capture_output=True, text=True\n    )\n    if compile_process.returncode != 0:\n        print(\"Java compilation failed:\", compile_process.stderr)\n        print(\"Please ensure javac is in your PATH and PY4J_JAR_PATH is correct.\")\n        # Fallback for systems without javac or if compilation fails: manual start instruction\n        print(\"Alternatively, you can manually compile and run the Java server:\")\n        print(f\"1. Save the Java code above as {java_file_name}\")\n        print(f\"2. Compile: javac -cp {PY4J_JAR_PATH} {java_file_name}\")\n        print(f\"3. Run: java -cp {PY4J_JAR_PATH}:. AdditionApplication\")\n        # If on Windows, use ';' instead of ':' for classpath separator in commands\n        raise RuntimeError(\"Java compilation failed, cannot run quickstart.\")\n\n    # Run Java Gateway Server in a separate process\n    # Using 'nohup' and '&' for background on *NIX, or 'start' on Windows\n    if os.name == 'nt': # Windows\n        java_server_command = [\"java\", \"-cp\", f\".;{PY4J_JAR_PATH}\", \"AdditionApplication\"]\n        java_process = subprocess.Popen(java_server_command, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)\n    else: # Unix-like\n        java_server_command = [\"java\", \"-cp\", f\":{PY4J_JAR_PATH}\", \"AdditionApplication\"]\n        java_process = subprocess.Popen(java_server_command, preexec_fn=os.setsid)\n\n    print(\"Starting Java Gateway Server...\")\n    time.sleep(5) # Give Java server time to start\n\n    # --- Python Client --- \n    gateway = JavaGateway(gateway_parameters=GatewayParameters(port=25333))\n\n    # Access the Java entry point (AdditionApplication instance)\n    addition_app = gateway.entry_point\n\n    # Call a Java method\n    number1 = 5\n    number2 = 3\n    result = addition_app.addition(number1, number2)\n    print(f\"Python calling Java: {number1} + {number2} = {result}\")\n\n    # Access JVM directly for standard Java objects\n    java_list = gateway.jvm.java.util.ArrayList()\n    java_list.add(\"Hello from Python!\")\n    print(f\"Java list created and modified from Python: {java_list}\")\n\nexcept Exception as e:\n    print(f\"An error occurred during quickstart: {e}\")\nfinally:\n    # Clean up Java process if it was started\n    if 'java_process' in locals() and java_process.poll() is None:\n        print(\"Shutting down Java Gateway Server...\")\n        if os.name == 'nt':\n            subprocess.run([\"taskkill\", \"/F\", \"/T\", \"/PID\", str(java_process.pid)], capture_output=True)\n        else:\n            os.killpg(os.getpgid(java_process.pid), 15) # Send SIGTERM\n        java_process.wait(timeout=5)\n    \n    # Clean up temporary files\n    os.remove(java_file_name) if os.path.exists(java_file_name) else None\n    os.remove(class_file_name) if os.path.exists(class_file_name) else None\n    # Also remove AdditionApplication$*.class files if they exist\n    for f in os.listdir('.'):\n        if f.startswith('AdditionApplication$') and f.endswith('.class'):\n            os.remove(f)\n","lang":"python","description":"This quickstart demonstrates how to connect a Python client to a Java Gateway Server, access an entry point object, and call Java methods. It includes a simplified Python-driven setup for the Java server (which usually runs independently). The example performs basic addition via a custom Java class and interacts with a standard Java `ArrayList`. Users *must* replace `/path/to/py4j<version>.jar` with the actual path to their Py4J JAR file for the Java compilation and execution to succeed.","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","last_tested":"2026-04-23","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]},"compatibility":{"tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","last_tested":"2026-05-19","installed_version":"0.10.9.9","pypi_latest":"0.10.9.9","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":100,"avg_install_s":1.7,"avg_import_s":0.18,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"py4j","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.11,"mem_mb":8.1,"disk_size":"18.6M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"py4j","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.17,"mem_mb":8.1,"disk_size":"18.6M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"py4j","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.6,"import_time_s":0.08,"mem_mb":8.1,"disk_size":"19M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"py4j","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.13,"mem_mb":8.1,"disk_size":"19M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"py4j","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.2,"mem_mb":8.6,"disk_size":"20.7M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"py4j","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.29,"mem_mb":8.6,"disk_size":"20.7M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"py4j","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.7,"import_time_s":0.18,"mem_mb":8.6,"disk_size":"21M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"py4j","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.28,"mem_mb":8.6,"disk_size":"21M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"py4j","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.17,"mem_mb":8.6,"disk_size":"12.5M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"py4j","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.21,"mem_mb":8.6,"disk_size":"12.5M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"py4j","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.6,"import_time_s":0.17,"mem_mb":8.6,"disk_size":"13M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"py4j","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.22,"mem_mb":8.6,"disk_size":"13M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"py4j","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.19,"mem_mb":8.7,"disk_size":"12.2M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"py4j","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.24,"mem_mb":8.7,"disk_size":"12.1M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"py4j","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.8,"import_time_s":0.18,"mem_mb":8.5,"disk_size":"13M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"py4j","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.26,"mem_mb":8.5,"disk_size":"13M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"py4j","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.13,"mem_mb":7.9,"disk_size":"18.1M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"py4j","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.19,"mem_mb":7.9,"disk_size":"18.1M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"py4j","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.8,"import_time_s":0.1,"mem_mb":7.9,"disk_size":"19M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"py4j","exit_code":0,"wheel_type":null,"failure_reason":null,"import_side_effects":null,"install_time_s":null,"import_time_s":0.14,"mem_mb":7.9,"disk_size":"19M"}]}}