Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

https://beeware.org/project/projects/bridges/rubicon/rubicon.png

Rubicon-Java

Python Versions Project version Project status License Build Status Discord server

Rubicon-Java is a bridge between the Java Runtime Environment and Python. It enables you to:

  • Instantiate objects defined in Java,

  • Invoke static and instance methods on objects defined in Java,

  • Access and modify static and instance fields on objects defined in Java, and

  • Write and use Python implementations of interfaces defined in Java.

Quickstart

Rubicon-Java consists of three components:

  1. A Python library,

  2. A JNI library, and

  3. A Java JAR file.

A Makefile has been provided to compile the JNI and JAR components. Type:

$ make

to compile them. The compiled output will be placed in the build directory.

To use Rubicon-Java, you’ll need to ensure:

  1. rubicon.jar is in the classpath when you start your Java VM.

  2. The Rubicon library file is somewhere that it will be found by dynamic library discovery. This means:

    1. Under OS X, put the directory containing librubicon.dylib is in your DYLD_LIBRARY_PATH

    2. Under Linux, put the directory containing librubicon.so is in your LD_LIBRARY_PATH

    3. Under Windows…. something :-)

  3. The rubicon Python module is somewhere that can be added to a PYTHONPATH. You can install rubicon using:

    $ pip install rubicon-java

    If you do this, you’ll need to reference your system Python install when setting your PYTHONPATH.

The Rubicon bridge starts on the Java side. Import the Python object:

import org.beeware.rubicon.Python;

Then start the Python interpreter, and run a Python file:

# Initialize the Python VM
String pythonHome = "/path/to/python";
String pythonPath = "/path/to/dir1:/path/to/dir2";
if (Python.start(pythonHome, pythonPath, null) != 0) {
    System.out.println("Error initializing Python VM.");
}

# Start a Python module
if (Python.run("path.of.module") != 0) {
    System.out.println("Error running Python script.");
}

# Shut down the Python VM.
Python.stop();

The PYTHONPATH you specify must enable access to the rubicon Python module.

In your Python script, you can then reference Java objects:

>>> from rubicon.java import JavaClass

# Wrap a Java class
>>> URL = JavaClass("java/net/URL")

# Then instantiate the Java class, using the API
# that is exposed in Java.
>>> url = URL("https://beeware.org")

# You can then call methods on the Java object as if it
# were a Python object.
>>> print(url.getHost())
beeware.org

It’s also possible to provide implementations of Java Interfaces in Python. For example, lets say you want to create a Swing Button, and you want to respond to button clicks:

>>> from rubicon.java import JavaClass, JavaInterface

# Wrap the Java interface
>>> ActionListener = JavaInterface('java/awt/event/ActionListener')

# Define your own implementation
>>> class MyActionListener(ActionListener):
...     def actionPerformed(self, event):
...         print("Button Pressed")

# Instantiate an instance of the listener
>>> listener = MyActionListener()

# Create a button, and set the listener
>>> Button = JavaClass('javax/swing/JButton')
>>> button = Button('Push it')
>>> button.setActionListener(listener)

Of course, this sample code won’t work unless it’s in the context of a larger application starting a Swing GUI and so on.

Testing

To run the Rubicon test suite:

  1. Ensure that java is on your $PATH, or set the JAVA_HOME environment variable to point to a directory of a Java Development Kit (JDK).

  2. Create a Python 3 virtual environment, and ensure that pip & setuptools are up to date:

    $ python3 -m venv venv
    $ source venv/bin/activate
    (venv) $ python -m pip install --upgrade pip
    (venv) $ python -m pip install --upgrade setuptools
  3. Install tox:

    (venv) $ python -m pip install tox
  4. Run the test suite. The following should work properly on both macOS and Linux:

    (venv) $ tox -e py

This will compile the Rubicon library, compile the Java test classes, and run the Python test suite from within the Java environment.

Documentation

Full documentation for Rubicon can be found on Read The Docs.

Community

Rubicon is part of the BeeWare suite. You can talk to the community through:

We foster a welcoming and respectful community as described in our BeeWare Community Code of Conduct.

Contributing

If you experience problems with this backend, log them on GitHub. If you want to contribute code, please fork the code and submit a pull request.

Metadata

Release files for rubicon-java 0.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rubicon-java 0.2.6
File Size Uploaded Provenance Actions
rubicon-java-0.2.6.tar.gz 83.5 kB Without provenance Details for rubicon-java-0.2.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rubicon-java 0.2.6
File Interpreter ABI Platform Actions
rubicon_java-0.2.6-py3-none-any.whl Python 3 none any Details for rubicon_java-0.2.6-py3-none-any.whl

Total release size: 109.4 kB

Release files / rubicon-java-0.2.6.tar.gz

Download URL rubicon-java-0.2.6.tar.gz
Size 83.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2ce7a7c2b80ef47a1b9ee0e6a41efced8973b0ec87e4dd62c2312f8e014ae007
BLAKE2b-256 checksum
How to use checksums
2c14a02a8bdb80e2a7e104cf95fd699d97e781cb9d2e0be72658e93a320557f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Release files / rubicon_java-0.2.6-py3-none-any.whl

Download URL rubicon_java-0.2.6-py3-none-any.whl
Size 25.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d0162b054184b8cf7a1016934df730bad335d9c74c322a01eafd30c742f350d6
BLAKE2b-256 checksum
How to use checksums
48d92f153e008ba79e0f380ea6a37f6831e84259562dc3ff3aefb686985d362e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Security for rubicon-java 0.2.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page