Giter Site home page Giter Site logo

paulproteus / rubicon-java Goto Github PK

View Code? Open in Web Editor NEW

This project forked from beeware/rubicon-java

0.0 1.0 0.0 300 KB

A bridge interface between Python and Java.

License: BSD 3-Clause "New" or "Revised" License

Python 60.45% C 27.52% Java 9.18% Makefile 2.85%

rubicon-java's Introduction

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

Rubicon-Java

Python Versions Project version Project status License Build Status Gitter chat room

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 dist directory.

Cross platform support

This Makefile currently only works under OS/X; however, the build commands aren't complicated; it should be fairly easy to reproduce the build on other platforms. Pull requests to make the Makefile cross-platform are welcome.

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 script
if (Python.run("/path/to/script.py") != 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("http://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. Configure your shell environment so that the Python, Java, and Rubicon dynamic libraries can be discovered by the dynamic linker.

    • On OSX, using Python 2.7.7 built under Homebrew:

      export DYLD_LIBRARY_PATH=/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/:`/usr/libexec/java_home`/jre/lib/server:./dist
      
  2. Build the libraries:

    $ make clean
    $ make all
    
  3. Run the test suite:

    $ java org.beeware.rubicon.test.Test
    

This is a Python test suite, invoked via Java.

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.

rubicon-java's People

Contributors

freakboy3742 avatar glasnt avatar jacebrowning avatar paulproteus avatar romankharin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.