Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Latest commit

 

History

History

Python to Java Translator

This module contains the code needed to translate Python’s bytecode into equivalent Java bytecode. This allows Java functions to use Python functions without incurring a large overhead cost on each function call for changing context from Java to Python (and then Python to Java). This is mostly relevant for small functions, such as those that simply return an attribute or perform a small calculation. For small functions, the performance increase is around 600%. As functions get larger, the overhead cost matters less, causing CPython to outperform the Java version.

Building

Install the python build module, then run the following command:

python -m build

Running

  1. Install the built javapython package into a virtual environment:

    python -m venv venv
    . venv/bin/activate
    pip install dist/jpyinterpreter-*-py3-none-any.whl
  2. Initialize the translator:

    import jpype.imports
    import jpyinterpreter
    
    jpyinterpreter.init(path=['my-java-jar.jar'])

    This will start the JVM used by the Java translator and load jars specified by the path parameter.

  3. Translate a Python function to Java:

    from java.util.function import Function
    
    def my_function(arg):
        return arg + 1
    
    translated_function = javapython.translate_python_bytecode_to_java_bytecode(my_function, Function)

    The first parameter is the Python function you want to translate, the second parameter is the Java class the function implements.

  4. Use the translated function like a normal Python function:

    from org.acme import MyClass # A class defined in a Java jar
    
    translated_function(10) # 11
    MyClass.performALongOperation(translated_function)