Getting started with Jython
Join the DZone community and get the full member experience.
Join For FreeA 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!
Published at DZone with permission of Simeon Franklin, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments