CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-jpype1

A comprehensive Python-to-Java bridge enabling seamless integration between Python and Java virtual machines

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

class-object-access.mddocs/

Java Class and Object Access

Access and manipulation of Java classes, objects, and packages through Python interfaces. This module provides structured access to Java APIs and seamless object interoperability between Python and Java.

Capabilities

Package Access

Gateway classes for structured access to Java packages and automatic class importation.

class JPackage:
    """Gateway for automatic importation of Java classes.
    
    Provides structured access to Java packages and classes.
    Sub-packages are created on demand.
    """
    
    def __init__(self, name: str, strict: bool = False):
        """Create a package gateway.
        
        Args:
            name: Package name (e.g., "java.lang", "com.example")
            strict: If True, raises error for missing packages
        """

# Global package instances
java: JPackage  # Gateway to java.* packages
javax: JPackage  # Gateway to javax.* packages

Class Management

Meta classes and factories for working with Java classes and creating instances.

class JClass:
    """Meta class for Java classes with type factory functionality.
    
    Provides access to Java class metadata, constructors, and static methods.
    """
    
    def __init__(self, name: str):
        """Load a Java class by name.
        
        Args:
            name: Fully qualified class name (e.g., "java.lang.String")
            
        Raises:
            TypeError: If class cannot be found or loaded
        """
    
    def __call__(self, *args, **kwargs):
        """Create new instance of the Java class.
        
        Args:
            *args: Constructor arguments
            **kwargs: Additional options
            
        Returns:
            JObject: New instance of the Java class
        """

class JInterface:
    """Base class for Java interfaces.
    
    Used to represent Java interface types and enable
    interface implementation from Python.
    """

def JOverride(func):
    """Decorator to mark method as overriding Java method.
    
    Args:
        func: Python method that overrides Java method
        
    Returns:
        func: The decorated method
    """

Object Manipulation

Base classes for working with Java objects and accessing their methods and fields.

class JObject:
    """Base class for all Java objects with casting functionality.
    
    Provides common methods for Java objects and type conversion.
    """
    
    def getClass(self):
        """Get the Java Class object for this instance.
        
        Returns:
            JClass: The Java Class object
        """
    
    def toString(self) -> str:
        """Get string representation via Java toString().
        
        Returns:
            str: String representation of the object
        """
    
    def equals(self, other) -> bool:
        """Check equality via Java equals() method.
        
        Args:
            other: Object to compare with
            
        Returns:
            bool: True if objects are equal
        """
    
    def hashCode(self) -> int:
        """Get hash code via Java hashCode() method.
        
        Returns:
            int: Hash code of the object
        """

# Direct access to internal classes
JMethod: type  # Direct access to _jpype._JMethod
JField: type   # Direct access to _jpype._JField

Usage Examples

Package Access

import jpype

jpype.startJVM()

# Using global package objects
String = jpype.java.lang.String
System = jpype.java.lang.System
ArrayList = jpype.java.util.ArrayList

# Create custom package gateway
mypackage = jpype.JPackage("com.example")
MyClass = mypackage.MyClass

jpype.shutdownJVM()

Class Loading and Instantiation

import jpype

jpype.startJVM()

# Load class explicitly
StringClass = jpype.JClass("java.lang.String")
ArrayListClass = jpype.JClass("java.util.ArrayList")

# Create instances
hello = StringClass("Hello World")
list_obj = ArrayListClass()

# Alternative: direct package access
String = jpype.java.lang.String
ArrayList = jpype.java.util.ArrayList

hello2 = String("Hello Again")
list_obj2 = ArrayList()

jpype.shutdownJVM()

Object Method Access

import jpype

jpype.startJVM()

# Create Java objects
string_obj = jpype.java.lang.String("Hello Java")
list_obj = jpype.java.util.ArrayList()

# Call Java methods
print(string_obj.length())  # 10
print(string_obj.toUpperCase())  # "HELLO JAVA"
print(string_obj.charAt(0))  # 'H'

# Collection operations
list_obj.add("item1")
list_obj.add("item2")
print(list_obj.size())  # 2
print(list_obj.get(0))  # "item1"

# Object comparison
string2 = jpype.java.lang.String("Hello Java")
print(string_obj.equals(string2))  # True
print(string_obj.hashCode())

jpype.shutdownJVM()

Static Method and Field Access

import jpype

jpype.startJVM()

# Access static methods
Math = jpype.java.lang.Math
result = Math.sqrt(16.0)  # 4.0
pi = Math.PI  # 3.141592653589793

# Access system properties
System = jpype.java.lang.System
java_version = System.getProperty("java.version")
System.out.println("Hello from Python!")

# Set system properties
System.setProperty("myapp.setting", "value")

jpype.shutdownJVM()

Class Metadata

import jpype

jpype.startJVM()

# Get class information
String = jpype.java.lang.String
string_obj = String("test")

# Access class object
clazz = string_obj.getClass()
print(clazz.getName())  # "java.lang.String"
print(clazz.getSuperclass().getName())  # "java.lang.Object"

# Check instance types
print(isinstance(string_obj, jpype.JObject))  # True

jpype.shutdownJVM()

Working with Nested Classes

import jpype

jpype.startJVM()

# Access nested classes using $ notation
Entry = jpype.JClass("java.util.Map$Entry")

# Or through package access
Map = jpype.java.util.Map
Entry = Map.Entry

jpype.shutdownJVM()

Install with Tessl CLI

npx tessl i tessl/pypi-jpype1

docs

advanced-features.md

class-object-access.md

classpath-management.md

exception-handling.md

index.md

interface-implementation.md

jvm-management.md

type-system.md

tile.json