Why use Jython when you could just use Java?

rbrayb picture rbrayb · Sep 18, 2008 · Viewed 17k times · Source

The standard answer is that it's useful when you only need to write a few lines of code ...

I have both languages integrated inside of Eclipse. Because Eclipse handles the compiling, interpreting, running etc. both "run" exactly the same.

The Eclipse IDE for both is similar - instant "compilation", intellisense etc. Both allow the use of the Debug perspective.

If I want to test a few lines of Java, I don't have to create a whole new Java project - I just use the Scrapbook feature inside Eclipse which which allows me to "execute Java expressions without having to create a new Java program. This is a neat way to quickly test an existing class or evaluate a code snippet".

Jython allows the use of the Java libraries - but then so (by definition) does Java!

So what other benefits does Jython offer?

Answer

e-satis picture e-satis · Sep 19, 2008

A quick example (from http://coreygoldberg.blogspot.com/2008/09/python-vs-java-http-get-request.html) :

You have a back end in Java, and you need to perform HTTP GET resquests.

Natively :

import java.net.*;
import java.io.*;

public class JGet {
    public static void main (String[] args) throws IOException {
        try {
            URL url = new URL("http://www.google.com");

            BufferedReader in = 
                new BufferedReader(new InputStreamReader(url.openStream()));
            String str;

            while ((str = in.readLine()) != null) {
                System.out.println(str);
            }

            in.close();
        } 
        catch (MalformedURLException e) {} 
        catch (IOException e) {}
    }
}

In Python :

import urllib
print urllib.urlopen('http://www.google.com').read()

Jython allows you to use the java robustness and when needed, the Python clarity.

What else ? As Georges would say...