Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting started with Jython

DZone's Guide to

Getting started with Jython

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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)
20000000000000000000000

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!

 

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Simeon Franklin, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}