Over a million developers have joined DZone.
Platinum Partner

Getting started with Jython

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix.  Discover how IT departments looking for ways to keep up with demand for business apps has caused a new breed of developers to surface - the Rapid Application Developer.

A student in my Python Fundamentals class asked me to demonstrate how to get started with Jython.

I've never used Jython and I'm not a Java programmer (processing doesn't count!) but I agreed to see if I could install it and demonstrate using a Java standard library class for him.

I didn't use my package manager to install but went the manual route, downloading the latest stable version from jython.org. This got me a file called jython_installer-2.5.2.jar. Did I mention I'm not a Java programmer? Still some vague memory prompted me to try:

$ java -jar jython_installer-2.5.2.jar

Aha! This popped up a graphical installer window! I selected my language, accepted the license, accepted the default installation components and chose an installation location of ~/bin/jython2.5.2. Then I was asked to pick a java home directory with the default being the current directory. I had no idea what this meant but noticed a second option of '/usr/lib/jvm/java-6-openjdk/jre' which existed and seemed a plausible choice.

The installer finished and I was off to see what new stuff I had in my install directory.

$ cd ~/bin/jython2.5.2
$ ./jython
Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06) 
[OpenJDK Server VM (Sun Microsystems Inc.)] on java1.6.0_20
Type "help", "copyright", "credits" or "license" for more information.

Of course now I have no idea what I can do with the Java standard library. I googled for "Java standard libary" and found myself at the official docs for Java. Oh yeah - I guess Oracle owns Java now... Let's see: Java.math.BigInteger looks interesting. Can I import it?

>>> from java.math import BigInteger
>>> x = BigInteger("10000000000000000000000")
>>> y = BigInteger("10000000000000000000000") 
>>> x + y
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for +: 'java.math.BigInteger' and 'java.math.BigInteger'
>>> dir(BigInteger)
['ONE', 'TEN', 'ZERO', '__class__', '__copy__', '__deepcopy__',
... snip...
'abs', 'add', 'and', 'andNot', 'bit', ...]
>>> x.add(y)

Yes, that is a big integer!

I also poked around in the demos a bit - Demo/awt/Colors.py is an 18 line Python program that looks pretty simple and running it:

$ jython Demos/awt/colors.py

reveals that it uses the AWT toolkit to put a bunch of labels with color names and the corresponding color in a gui window. Jython seems to do what it says on the label - lets me talk to Java classes but with the Python runtime - and it seems simple enough to get started on. Good luck!


The Web Dev Zone is brought to you in partnership with Mendix.  Learn more about The Essentials of Digital Innovation and how it needs to be at the heart of every organization.


Published at DZone with permission of Simeon Franklin , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}