Did you know: programmers convert coffe to code?

If you like my articles, support me with a small amount.


Buy me a coffee

PyJa — calling Java from Python part 3

This is another article in the long series for calling Java from Python. Now I’ll take a look at Jython what I missed in the last article.

Jython

If we arrived at this point I have to tell some words about Jython.

Jython is a Python implementation created in Java. So the installation of Jython differs from the other libraries mentioned in the previous articles: it does not come as an extension library for Python. This is because the “normal” Python (you call at your command line wit the command ‘python’) is the CPython implementation — written in C. The third mainstream implementation of the Python language is in .NET and is called IronPython. So if you here somewhere the word “Python” then CPython is mentioned — this is the case in my own articles too.

After reading through the Installation section you will think I should have called the article JyJa — calling Java from Jython. This would make sense however I’ll stay with Python because Jython implements the Python language so it is OK to let the article’s title stay as it is. Now this is an exception from my words above (stating that “Python” references for my for “CPython”).

Installation

Just simply download the installation JAR and run it. You have to click through some steps and you are done. As for writing this article I’ll take the 2.5.3 version.

After the installation you have two choices to start the Jython interpreter: 1) execute the jython.sh or jython.bat always from your installation directory 2) add the installation directory to your path environment variable. I’d suggest the latter, because so you can call Jython from any folder.

Examples

I’ll stick with the same examples as in the previous two articles.

I created a Java class named JythonExample as follows:

/**
 * @author GHajba
 */
public class JythonExample {

    public static int addition(Integer... numbers) {
        int sum = 0;
        for(int i : numbers) {
            sum += i;
        }
        return sum;
    }

    public static int addition(String value) {
        return value.hashCode();
    }

    public int addNumbers(int a, int b, int c, int d) {
        return a+b+c+d;
    }
}

You can see, I have the two static methods which are overloaded (same return type, same name, different arguments!) and a non-static method with Integer var-args as parameter. I compiled the sources with Java 1.8 — to see if Jython can handle such a “new” version.

First of all let’s start the Jython interpreter with the “jython” command in the same folder where your compiled Java class lies (I assume you’ve added the installation folder to your PATH).

After this let’s import and instantiate the JythonExample class and call some methods:

Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36) 
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_05
Type "help", "copyright", "credits" or "license" for more information.
>>> import JythonExample
>>> jythonExample = JythonExample() # instantiate the class
>>> jythonExample.addNumbers(13,1,20,8)
42
>>> jythonExample.addition(13,1,20,8)
42
>>> jythonExample.addition("Hello World")
-862545276
>>> JythonExample.addition(13,1,20,8) # static call
42
>>> JythonExample.addition("Hello World")
-862545276
>>> JythonExample.addNumbers(13,1,20,8)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: addNumbers(): expected 5 args; got 4
>>> 

As you can see Jython does not have any problems with boxing and unboxing, method overloading. The only problem is, that you need a Jython interpreter to run your code — it does not work from a Python interpreter.

Or if you have a Jython script and a standalone version, you can call

java -jar jython-standalone-2.5.3.jar jythonExample.py

to execute the script. Naturally you can call your script with the Jython interpreter too:

jython jythonExample.py

Both calls yield the same result:

jythonExample.addNumbers(13,1,20,8):
42
jythonExample.addition('Hello World'):
-862545276
JythonExample.addition(13,1,20,8): 
42
JythonExample.addition('Hello World'):
-862545276

I included the Jython script and the standalone version of Jython 2.5.3 in the repository for this article.

Conclusion

If you want to use a recently Java version (1.7 and above) and can afford to have Jython on the machine executing your code I’d say that you should use Jython. If you cannot install Jython on the target machine, then try calling Jython’s standalone JAR file. If it is not possible to do this too and you only have Python (CPython of course) and you have to call Java classes, then stay with one of the previously mentioned libraries (my suggestion is PyJNIus).

GHajba
 

Senior developer, consultant, author, mentor, apprentice. I love to share my knowledge and insights what I achieve through my daily work which is not trivial -- at least not for me.

Click Here to Leave a Comment Below 0 comments