A comprehensive Python-to-Java bridge enabling seamless integration between Python and Java virtual machines
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core functionality for starting, configuring, and managing the Java Virtual Machine from within Python applications. This module provides complete control over JVM lifecycle, configuration, and discovery.
Start and stop the Java Virtual Machine with custom configuration options.
def startJVM(*jvmargs: str, jvmpath=None, classpath=None, ignoreUnrecognized=False, convertStrings=False, interrupt=True):
"""Start the Java Virtual Machine.
Args:
*jvmargs: JVM arguments like "-Xmx1g", "-Djava.awt.headless=true"
jvmpath: Path to JVM library file (libjvm.so, jvm.dll, etc.)
classpath: String path or list of paths to set classpath
ignoreUnrecognized: Ignore invalid JVM arguments (default False)
convertStrings: Force Java strings to convert to Python str (default False)
interrupt: Enable interrupt handling (default True)
Raises:
OSError: If JVM cannot be started
RuntimeError: If JVM is already running
"""
def shutdownJVM():
"""Shutdown the running JVM.
Raises:
RuntimeError: If JVM is not running
"""
def isJVMStarted() -> bool:
"""Check if JVM is currently running.
Returns:
bool: True if JVM is started, False otherwise
"""Discover and configure JVM installations and retrieve runtime information.
def getDefaultJVMPath() -> str:
"""Find the default JVM library path on the system.
Returns:
str: Path to the default JVM library
Raises:
JVMNotFoundException: If no JVM is found
"""
def get_default_jvm_path() -> str:
"""Alternative name for getDefaultJVMPath().
Returns:
str: Path to the default JVM library
"""
def getJVMVersion() -> tuple:
"""Get the version of the running JVM.
Returns:
tuple: JVM version as (major, minor, patch)
Raises:
RuntimeError: If JVM is not running
"""Legacy threading functions for manual thread management. Modern JPype automatically manages thread attachment.
def isThreadAttachedToJVM() -> bool:
"""Check if current thread is attached to JVM.
Returns:
bool: True if thread is attached
Note:
Deprecated: JPype automatically manages thread attachment
"""
def attachThreadToJVM():
"""Manually attach current thread to JVM.
Note:
Deprecated: JPype automatically manages thread attachment
"""
def detachThreadFromJVM():
"""Detach current thread from JVM.
Note:
Deprecated: JPype automatically manages thread attachment
"""Control JVM initialization timing and register startup callbacks.
def onJVMStart(func):
"""Decorator to register function to be called after JVM starts.
Args:
func: Function to call when JVM starts
Returns:
func: The original function (for use as decorator)
Example:
@onJVMStart
def load_resources():
# Load Java classes or resources
pass
"""Exceptions raised during JVM operations.
class JVMNotFoundException(RuntimeError):
"""Raised when JVM installation cannot be found."""
class JVMNotSupportedException(RuntimeError):
"""Raised when JVM version is not supported."""
class JVMNotRunning(RuntimeError):
"""Raised when JVM operations attempted while not running."""import jpype
# Start JVM with default settings
jpype.startJVM()
# Check if running
if jpype.isJVMStarted():
print("JVM is running")
# Get JVM version
version = jpype.getJVMVersion()
print(f"JVM version: {version}")
# Shutdown when done
jpype.shutdownJVM()import jpype
# Start with custom memory and system properties
jpype.startJVM(
jpype.getDefaultJVMPath(),
"-Xmx2g", # Set max heap to 2GB
"-Xms512m", # Set initial heap to 512MB
"-Djava.awt.headless=true", # Headless mode
classpath=["lib/mylib.jar", "classes/"],
convertStrings=False # Keep Java strings as JString objects
)
# Your Java integration code here
java = jpype.java
String = java.lang.String("Hello Java!")
jpype.shutdownJVM()import jpype
@jpype.onJVMStart
def initialize_resources():
"""Load resources after JVM starts."""
# Load Java classes that depend on JVM
MyClass = jpype.JClass("com.example.MyClass")
MyClass.initializeStaticResources()
# Start JVM - callback will be executed automatically
jpype.startJVM()
jpype.shutdownJVM()import jpype
try:
jvm_path = jpype.getDefaultJVMPath()
print(f"Found JVM at: {jvm_path}")
jpype.startJVM(jvm_path)
except jpype.JVMNotFoundException:
print("No JVM found on system")
except jpype.JVMNotSupportedException:
print("JVM version not supported")Install with Tessl CLI
npx tessl i tessl/pypi-jpype1