Jython – Combination of Java and Python

This post is used for final project of CIS610 – Teaching Effectiveness in CIS dept at UO, dedicated to the students with background of CIS122 – Intro to Algorithms and Problem Solving Using Python. Background of Java is not mandatory but would be helpful for writing Jython code in Java. Any comments please email: [daveti@cs.uoregon.edu] May it help:)

1. What is Jython?

Literally, Jython means Java + Python, which means writing Python code using Java code or writing Java code in Pythonic style (Python syntax). However, it is much more than that and we will see it later. Generally speaking, Jython is a kind of dynamic scripting languages, like Python or Ruby. Further more, it has the same syntax of Python, like using ‘:’ and indentation to stand for block instead of ‘{}’. Technically speaking, Jython is another kind of JVM-based languages, like Scala or Groovy. Essentially, in a word, Jython is scripting language running on JVM with Python syntax and library support from both Java and Python.

2. Jython interpreter/compiler

As Python, Jython has its interpreter to do interactive programming and compiler to compile and run the Jython source file, though the interpreter and compiler are integrated as one binary. As Jython itself is written in Java and Python and is JVM-based, the Jython interpreter/compiler is a big jar file running on JVM actually, named ‘jython.jar’, no matter ‘jython.sh’ under Unix/Linux or ‘jython.bat’ under Win/Dos is executed, interpreting and compiling the Jython code into bytecode running on this JVM.

3. Architecture of Jython

4.0 Key features of Jython

In the following paragraphs, we will try to go through the most key features of Jython, through which we could understand the reason why Jython was created and why it is so powerful and popular, as well as its limitation. We will give at least one example for each key feature helping understand without going deep into the implementation of these features of Jython. NOTE: all the examples are using compiler mode of Jython with source files instead of interpreter mode to make it more clear. However, the 2 modes are equivalent! All the example codes below could be downloaded from http://ix.cs.uoregon.edu/~daveti/jythonEx/

4.1 Writing Java like Python

If you are familiar with both Java and Python, then the most probable language you hate (at least I hate:) between the 2 would be Java. Why? And the most probable reasons (at least for me:) would be the redundant syntax (please recall how many lines of code you need to print the stupid ‘hello world!’) and no scripting support (though we have many other JVM-based scripting languages). Check the ‘hello world’ below, does it look better comparing with original Java version?

A complex example below involves Java GUI programming with Pythonic coding style – Python syntax.

4.2 Writing Jython as Python

Actually, you will find all the source files of Jython have the same suffix as Python source files, with the format ‘*.py’. This is not coincidence but provides us the ability to write Jython as Python, as well as compiling Jython using Python interpreter/compiler, which will be mentioned later. Check the new Pythonic style of ‘hello world’. Feel free to wonder if this Jython or Python:)

A complex example below defines a new class inherited from ‘dictionary’ in Python and adds some new methods.

4.3 Writing Jython calling/using Java and Python

If you were asking what is the essential feature/ability of Jython, from my personal point of view, this is – the combination of Java and Python into Jython. The example below defines a function called ‘swingTest()’, which creates a Java Swing window using javax.swing,  and converts the Python ‘list’ into the content of the Java ‘Panel’.

The other example is the interactive mode of Jython using interpreter, just like using Python interpreter:

4.4 Writing Java calling/using Jython/Python

According to the Jython user guide, there are 2 ways to accomplish this job, using org.python.core pkg or Java JSR 223 pkg. Both ways need the classpath to include your ‘jython.jar’ file, which contains both the 2 pkgs. Before starting, we will give a simple Jython/Python script and run it using Jython compiler.

First, we will use org.python.core pkg provided by Jython itself. We will give the Java code and run it using JVM.

Second, we will use Java JSR 223 which is also contained in Jython itself.

5. Writing Python calling/using Java (Optional)

Actually, this is not part of Jython. The only reason we put it here is to complete the connection between Java and Python and we will NOT provide detailed code examples for this part. Generally, there 2 popular Python pkgs providing us such an ability – writing Python calling Java: ‘Jpype’ and ‘Py4J’. Detailed info please refer to ‘9. Reference’. Moreover, ‘Jpype’ seems only support older version of Python, like Python 2.6 and is not updated for long (the latest version is from 2011). Because of these, ‘Py4J’ may be a better choice.

6. Performance of Jython

If you were worried about the performance about Jython, then you were asking the performance of JVM eventually. Comparing with the running time, the parsing, compiling time of Jython, which is from Jython/Python code to Java bytecode, is trivial. Then the real performance issue, like we mentioned above, is laying on JVM itself. The challenge for JVM is non-trivial but…especially concerning the improving JIT compiler and garbage collector within JVM. In a word, you could always try to write certain part of your code in Java (hoping to get better performance, which is mostly rely on JVM) and then integrate that part into your Jython script.

7. Limitations of Jython

The reason we could write Python code using the basic data structure, like list or dictionary, and almost all the standard pkgs of Python, like sys, shutil, date, is that Jython has provided us these Python features in pre-compiled/implemented Java class files, which are bytecode, already. However, for Python’s extension, like 3rd-party cool pkgs, we can do nothing at all in Jython. Though Jython has the ability to access all kinds of Java libraries, it is true that its Python ability is always far behind Python.

8. Hints

If you are using the Chinese version OS, whose encoding system may be ‘GBxxxxx’, then your Jython is not compatible with this encoding. You need to change the encoding to ‘UTF-8’, either via Jython interpreter, JVM start-up options or registry file under Jython installation directory. Detailed procedure please refer to the link: http://cpaul.is-programmer.com/posts/15226.html

9. Reference


About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Programming and tagged , , , , . Bookmark the permalink.

1 Response to Jython – Combination of Java and Python

  1. Jeff Kish says:

    thanks for the information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.