Wednesday, February 9, 2011

Jython - Simple Setup and Example

Jython (http://www.jython.org/) is a Python for Java project. It is useful for people who want to run Python code in JVM.

Here is to teach you some simple set up.

1. Make sure you download latest Jython at http://www.jython.org/downloads.html. When I post this, the latest version is 2.5.3.RC3

2. The 2 most important file in the download is jython.jar and the Lib folder. jython.jar only provide a small implementation of Python functions, seehttp://www.jython.org/javadoc/index.html. Thus, you have to include the provided Lib folder. As long as the Lib folder exist along jython.jar, you can import the provided Python libraries. For example, "from re import *" to import Python regular expression library.

3. Write your own Python code with .py extension

4. Then, you can run this Python code with Java by calling

java -cp "jython.jar" org.python.util.jython your_python_code.py

No comments:

Post a Comment

Hadoop - How to setup a Hadoop Cluster

Below is a step-by-step guide which I had used to setup a Hadoop Cluster Scenario 3 VMs involved: 1) NameNode, ResourceManager - Host...