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 can be quicker to see what this is about by seeing some examples.
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:
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
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
Pick one of the following:
It isn't all great, however. Here are some reasons why this might not do what you want it to: