Jython is a port of the Python language to the Java virtual machine. Compared to CPython, it means that some library that use C implementation won't be available (e.g. Numpy). However it can readily access Java classes and provides conversion for all the basic types between the Java and Python world. The nice thing about Jython is that it gives an interpreter to play with Java libraries.
In order to install and run Jython:
- You will need a Java JRE
- Download Jython, either the stand-alone version or the installer. I use the stand-alone version below to reduce the installation footprint.
- If you used the installer, there's a jython.exe command in the jython/bin directory. If you use the installer, the command will be:java -jar <path to the jar>\jython-standalone-2.7.0.jar <other command line arguments>
- Add additional .jar as required. In my case, I add to install 2 jars from Oracle KVStore. There are several ways to do it.
- the first option is to add all the .jar files to the environment variable JYTHONPATH, before starting jython.
- Or add it to the sys.path directly from the program file. For instance:
import sys import os jar_dir = <replace this by the path to the vendor jars directory> for i in os.listdir(jar_dir): sys.path.append(os.path.join(jar_dir,i))
And then it's easy to mix Java and Python code:
from java.util import ArrayList a=ArrayList() a.add(1) print a