javaclass

The javaclass collection of packages and utilities (also known as ClassFile) provides a means of importing Java classes and packages directly into Python, without the need for a Java virtual machine, so that the classes may be instantiated, accessed, run and manipulated just like Python classes, and that the resulting objects and methods can be accessed and manipulated just like Python objects and methods. It should be possible to run compiled Java programs with the Python interpreter and not notice that it isn't the Java virtual machine being used - given sufficient library support for the program concerned, of course.

Quick Examples

It can be quicker to see what this is about by seeing some examples.

The Not Very Convincing Example

You can run Java classes by finding one with a main method and executing it. Here's a comparison of a freshly prepared Java class being run in Python and in a Java virtual machine respectively:

cd tests/
javac Value.java
runclass.py Value
v.getValue() correct: 123
v.getValue() correct: 456
v.isPositive() correct: 1
v.isPositive() correct: 0
v.compare(-790) correct: -1
v.compare(-788) correct: 1
v.compare(-789) correct: 0
v.getValue() == v2.getValue() correct: 0
v2.add(-123) correct: 0
v2.getValue() correct: 255
java Value
v.getValue() correct: 123
v.getValue() correct: 456
v.isPositive() correct: true
v.isPositive() correct: false
v.compare(-790) correct: -1
v.compare(-788) correct: 1
v.compare(-789) correct: 0
v.getValue() == v2.getValue() correct: false
v2.add(-123) correct: 0
v2.getValue() correct: 255

The Slightly More Credible Example

It can be more interesting to get into Python's interactive mode and then start playing around with Java classes:

Python 2.2.2 (#2, Jan 21 2005, 16:16:57)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import javaclass.classhook
from __this__ import Value
dir()
['Value', '__builtins__', '__doc__', '__name__',
'javaclass']
dir(Value)
['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__init______I_', '__module__',
'__new__', '__reduce__', '__repr__', '__setattr__', '__str__', '__weakref__',
'add', 'add____I_', 'compare', 'compare____I_', 'getClass', 'getClass___',
'getValue', 'getValue___', 'isPositive', 'isPositive___', 'main',
'main___java__lang__String_array_', 'newValue', 'newValue___', 'setValue',
'setValue____I_']
v = Value(20050121)
v.getValue()
20050121
v.setValue(20050401)
v.getValue()
20050401

Copyright and Licence

The javaclass software is distributed under the terms of the GNU Lesser General Public Licence (LGPL). See the file COPYING.txt in the docs directory within the source code distribution.

Getting Started

See the README.txt file in the distribution directory.

Motivation

Pick one of the following:

Limitations

It isn't all great, however. Here are some reasons why this might not do what you want it to:

Suggestions for Python Improvements