JVM bindings for Python that enable seamless integration between Python and Java code through JNI (Java Native Interface).
- Direct JNI Integration: Low-level JNI bindings using ctypes for maximum performance
- Dynamic Class Discovery: Automatic discovery of Java classes, methods, and fields using reflection
- Python Import Hook: Use standard Python import syntax to access Java classes
- Type Conversion: Automatic conversion between Python and Java types
- Memory Safety: Proper JNI reference management and safe shutdown procedures
- Cross-Platform: Support for Windows, macOS (including ARM64), and Linux
- Configuration: Flexible configuration through pyproject.toml
pip install jvm-pybind
# Import Java classes using standard Python syntax
from java.lang import System
# Call Java methods directly
System.out.println("Hello from JVM!")
# Access Java properties
print(f"Java Version: {System.getProperty('java.version')}")
print(f"Java Vendor: {System.getProperty('java.vendor')}")
jvm-pybind provides a CLI for managing Java type stubs in your development environment.
Install type stubs to enable IDE support and autocompletion:
# Install Java type stubs to current virtual environment
python -m jvm --install-stub
Remove type stubs when no longer needed:
# Remove Java type stubs from current virtual environment
python -m jvm --uninstall-stub
Install a .pth file to enable automatic JVM import in your virtual environment:
# Install jvm.pth file for automatic import
python -m jvm --install-pth
This creates a jvm.pth
file in your virtual environment's site-packages
directory that automatically imports the JVM module when Python starts, enabling seamless Java class imports without manual initialization.
Type Stub Management:
- Install stubs: Automatically detects your virtual environment and installs Java type stubs for better IDE support
- Uninstall stubs: Cleanly removes all installed Java type stubs
- Auto-generation: Generates fresh stubs from your JVM installation if needed
- Virtual environment detection: Works with venv, virtualenv, conda, and other Python environment managers
PTH File Management:
- Install PTH file: Creates a
jvm.pth
file in your virtual environment that automatically imports the JVM module at Python startup - Automatic import: Enables seamless Java class imports without manual JVM initialization
- Virtual environment safety: Only works within active virtual environments for isolated setup
Supported Packages:
java.lang
- Core Java classes (String, System, Object, etc.)java.util
- Collections and utilities (List, Map, ArrayList, etc.)java.io
- Input/output classes (File, InputStream, OutputStream, etc.)
- Virtual Environment: CLI operations require an active virtual environment
- JVM Installation: Java must be installed and accessible for stub generation
# Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install jvm-pybind
pip install jvm-pybind
# Install type stubs for IDE support
python -m jvm --install-stub
# OR install PTH file for automatic JVM import
python -m jvm --install-pth
# Now you get autocompletion in your IDE
from java.lang import System # IDE will show available methods
python -m jvm --help
Output:
usage: jvm [-h] (--install-stub | --uninstall-stub | --install-pth)
JVM-PyBind: Python bindings for JVM with type stub management
options:
-h, --help show this help message and exit
--install-stub Install JDK type stubs to the current virtual environment
--uninstall-stub Remove JDK type stubs from the current virtual environment
--install-pth Install jvm.pth file to enable automatic JVM import in
virtual environment
Examples:
python -m jvm --install-stub Install JDK type stubs to virtual environment
python -m jvm --uninstall-stub Remove JDK type stubs from virtual environment
python -m jvm --install-pth Install jvm.pth file to enable automatic JVM import
⚠️ Note: Custom Java class access is currently experimental. While JAR files can be included in the classpath during JVM startup, direct access to custom classes through Python import syntax is not yet fully implemented.
Current Capability:
# pyproject.toml - JAR files are loaded into JVM classpath
[tool.jvm]
java-version = "17"
classpath = ["hello.jar"]
Planned Feature (Not Yet Available):
# This will be supported in future versions
from mypkg import Hello # Not yet implemented
message = Hello.greet("World")
Current Workaround: Use the internal API to access custom classes:
import jvm
# Get JVM instance
jvm_instance = jvm.JVM.get_instance()
# Find your custom class
hello_class = jvm_instance.find_class("mypkg/Hello")
# Access methods through low-level API
# (See Internal API section below)
Configure jvm-pybind through your pyproject.toml
file:
[tool.jvm]
java-version = "17" # Java version to use
classpath = [ # JAR files and directories to include
"path/to/your.jar",
"path/to/classes/"
]
[tool.jvm.deps]
maven = [ # Maven dependencies (future feature)
"org.apache.commons:commons-lang3:3.12.0"
]
- Java 17 (recommended, configurable)
- Supported JDK distributions:
- Oracle JDK
- Eclipse Adoptium (formerly AdoptOpenJDK)
- Amazon Corretto
- Microsoft Build of OpenJDK
- Azul Zulu
- OpenJDK
- Python 3.12+
- Supported platforms:
- Windows (x64)
- macOS (Intel and Apple Silicon)
- Linux (x64, ARM64)
jvm-pybind consists of several key components:
- JVMLoader: Initializes the JVM and loads the libjvm library
- JNIHelper: Low-level JNI function bindings with type safety
- JVM: Main interface for Java class discovery and method execution
- Proxy Classes: Python wrappers for Java packages, classes, and objects
- Import Hook: Integration with Python's import system
import jvm
# Get the JVM instance
jvm_instance = jvm.get_jvm()
# Find a Java class
string_class = jvm_instance.find_class("java.lang.String")
# Access class information
print(f"Methods: {len(string_class.methods)}")
print(f"Fields: {len(string_class.fields)}")
The library automatically manages JNI references, but you can also control memory explicitly:
from java.lang import System
# The JVM will be automatically shut down when Python exits
# For explicit control:
jvm.shutdown()
📋 Note: This section documents the internal API for advanced users and developers. For most use cases, the high-level import syntax (
from java.lang import System
) is recommended.
import jvm
# Get the current JVM instance (if running)
jvm_instance = jvm.get_jvm() # Returns None if JVM not started
# Start JVM with custom configuration
from jvm.config import Config
from jvm.loader import JVMLoader
config = Config(java_version="17", classpath=["path/to/jar"], deps={})
jvm_instance = JVMLoader(config).start()
# Find a Java class by name
java_class = jvm_instance.find_class("java/lang/String")
print(f"Class: {java_class.name}")
print(f"Methods: {len(java_class.methods)}")
print(f"Fields: {len(java_class.fields)}")
# Access class methods and fields
for method in java_class.methods:
print(f"Method: {method.name}({', '.join(method.parameters)}) -> {method.return_type}")
print(f"Static: {method.is_static}")
# Access the underlying JNI helper
jni = jvm_instance.jni
# Find class and get method ID
string_class = jni.FindClass("java/lang/String")
length_method = jni.GetMethodID(string_class, "length", "()I")
# Create a Java string
java_str = jni.NewStringUTF("Hello World")
# Call method
length = jni.CallIntMethod(java_str, length_method)
print(f"String length: {length}")
# Discover classes in a package
classes = jvm_instance.discover_package_classes("java.util")
for class_name in classes:
print(f"Found class: {class_name}")
from jvm.proxy import ClassProxy, PackageProxy
# Create proxy for a Java package
java_lang = PackageProxy(jvm_instance, "java.lang")
system_class = java_lang.System # Returns ClassProxy
# Access static methods
system_class.gc() # Calls System.gc()
property_value = system_class.getProperty("java.version")
from jvm.config import Config
# Load configuration from pyproject.toml
config = Config.from_pyproject()
print(f"Java Version: {config.java_version}")
print(f"Classpath: {config.classpath}")
print(f"Dependencies: {config.deps}")
# Create custom configuration
custom_config = Config(
java_version="11",
classpath=["/path/to/custom.jar"],
deps={"maven": ["org.apache.commons:commons-lang3:3.12.0"]}
)
from jvm.typeconv import to_java, to_python
# Convert Python values to Java
java_string = to_java(jvm_instance, "Hello")
java_int = to_java(jvm_instance, 42)
java_bool = to_java(jvm_instance, True)
# Convert Java values to Python
python_value = to_python(jvm_instance, java_string)
from jvm.jvm import JNIException
try:
# JNI operations that might fail
unknown_class = jvm_instance.find_class("com/nonexistent/Class")
except JNIException as e:
print(f"JNI error: {e}")
The main classes you can import and use:
Class | Purpose | Example Usage |
---|---|---|
jvm.JVM |
Main JVM interface | jvm_instance.find_class() |
jvm.JNIHelper |
Low-level JNI functions | jni.FindClass() |
jvm.Config |
Configuration management | Config.from_pyproject() |
jvm.JVMLoader |
JVM initialization | JVMLoader(config).start() |
jvm.proxy.* |
Java object proxies | ClassProxy() , ObjectProxy() |
# Clone the repository
git clone https://github.com/t3tra-dev/jvm-pybind.git
cd jvm-pybind
# Initialize environment
./reinstall.sh # Or manually create a virtual environment
# Run the example
cd examples/hello
python main.py
jvm-pybind/
├── src/jvm/ # Main package
│ ├── __init__.py # Package initialization
│ ├── jvm.py # JVM interface
│ ├── jni.py # JNI bindings
│ ├── loader.py # JVM loader
│ ├── proxy.py # Java object proxies
│ ├── config.py # Configuration management
│ ├── typeconv.py # Type conversion utilities
│ └── import_hook/ # Python import hook
├── examples/ # Usage examples
└── tests/ # Test suite
boolean
↔bool
int
↔int
long
↔int
float
↔float
double
↔float
String
↔str
- Java objects are wrapped in proxy classes
- Arrays and collections (planned)
- Custom classes through reflection
- JVM Startup: The JVM is initialized lazily on first Java import
- Memory Usage: JNI references are managed automatically
- Method Calls: Direct JNI calls for optimal performance
- ARM64 Optimization: Special optimizations for Apple Silicon
- Java not found: Ensure Java is installed and
JAVA_HOME
is set - ClassNotFoundException: Check your classpath configuration
- Memory errors: Verify adequate heap space for your application
Enable debug logging:
import logging
logging.basicConfig(level=logging.DEBUG)
from java.lang import System # Will show debug output
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Python's ctypes for JNI integration
- Inspired by JPype and similar Java-Python bridge projects
- Special thanks to the Python and Java communities
- Custom Java class import support - Enable
from mypkg import MyClass
syntax for custom classes - Enhanced type conversion - Support for more Java types (arrays, collections, etc.)
- Comprehensive test suite - Full test coverage for all features
- Maven dependency resolution - Automatic downloading and management of Maven dependencies
- Java collection support - Native Python integration with Java Lists, Maps, etc.
- Performance optimizations - Method call optimization and caching
- Callback support - Enable Java code to call Python functions
- Advanced debugging tools - Better error messages and debugging capabilities
- IDE integration - Type hints and autocompletion for Java classes