Reg Jython Python wrap for Java

267 Views Asked by At

I have been assigned a task to convert a java standalone application to python web application.Recoding the entire module in python language would take a lot of time and effort.Hence I was adviced do a quick wrap up of python and get the code working (jython.org) (Jython is Python wrap for JAVA )..Could any one guide me how to get started as I am new to Python as well as Jython?

1

There are 1 best solutions below

0
On

To get you started:

If you're familiar with Java, then you should be able to get to the Jython prompt no problem. Just execute it like any other .jar. If you didn't download the standalone jython.jar, be sure to include the Jython libraries in your classpath.

Say your Java application's package is named com.stackoverflow.q10715162, and is compiled as a .jar in C:\jars\your_app.jar.

Then, you can get access to its classes in Jython. At the Jython prompt:

>>> import sys
>>> sys.path
['', 'C:\\jython\\Lib', 'C:\\jython\\jython.jar\\Lib', '__classpath__', 
'__pyclasspath__']

Here, sys.path is, among other things, a list of directories where your Jython distribution is looking for compiled modules. By adding your compiled Java application to the list, it will become accessible (more in-depth info is available for this at http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html):

>>> sys.path.append('C:\\jars\\your_app.jar')
>>> import com.stackoverflow.q10715162 as yourapp
*sys-package-mgr*: processing new jar, 'C:\\jars\\your_app.jar'
>>> dir(yourapp)
['Class1', 'Class2', 'Class3', ...]

By using dir(yourapp) you can see the classes you defined in your Java application. dir(yourapp.Class1) will list all methods, functions, etc. that are within the class.

You probably want to read through the first few pages at least of the Jython Book to familiarize yourself with the new syntax. I find it much simpler than Java's.

For making a Jython web app, I've heard cgi is by far the fastet way to get started with the least overhead:

#!/usr/bin/python

print("Content-Type: text/plain\n\n")

print("Hello, World!\n")

This tutorial seems helpful: http://www.cs.virginia.edu/~lab2q/lesson_1/. Although it is for Python, almost all of it should be applicable to Jython.

And of course, there are many other Python/Jython web service options if cgi doesn't suit you or your project. I've used web2py and really liked it.