Godson's Blog

Syndicate content
A Web-log on VoIP, CTI, Asterisk, FreeSWITCH & Python in India!Godson Ghttps://plus.google.com/100965631603901259030noreply@blogger.comBlogger33125
Updated: 1 year 8 weeks ago

Simple example using Jython and JPype

Mon, 11/23/2015 - 01:46
In this post I am going to show you simple example of getting a random integer using java.util.Random in both JPype and Jython

Random.nextInt() using JPype : -

The startJVM() call is important here. We need to invoke JVM before accessing any java functions. We need to supply java path to startJVM. The convenient function of getDefaultJVMPath() returns the path of default JVM installed on your machine.

Random.nextInt() using Jython:-

Unlike JPype you can import the java module using familiar Python import syntax.

As shown in the above examples. Both JPype and Jython opens up access to rich set of java libraries to Python world in a simple and easy way. 
Categories: Blog

Taking advantage of Java libs using Python

Mon, 11/23/2015 - 01:45
If you want to take advantage of rich set of java libraries but, without leaving your favorite language Python. You have couple of options.

1. JPype - http://jpype.sourceforge.net/
2. Jython - http://www.jython.org/

Both the above projects allow to call Java code using Python syntax. But, there is distinction how they achieve this.

How they are implemented ?: -

JPype is implemented as module in CPython. So all you need to do is install JPype package and you are ready to go. On the other hand Jython is a implementation of Python language syntax using Java. Both, have their advantages.

Which one should I pick ? : -

JPype is a suitable candidate if your application depends on lots of other external Python packages that are not available in Jython world. Like, for example you have an  application built using PySide/PyQt and you want to call some java functions from within that application. Then, JPype is your best bet. This way JPype offers you best of the both worlds Python and Java.

If you want to build your application purely using Java libraries but don't want to leave Python syntax , then Jython helps you out here. As Jython is implementation of Python language specification using Java. Often, it tends to lag behind in terms of features/standard library modules available in CPython implementation of Python.  Another use of Jython is, if you have pure java application and you want to bring Python scripting features to it. Then, you can use jython.jar to embed in your program. This will help users to extend you program by writing scripts or plugins.

Show me the code :-

A simple helloworld program example is shown in next post -  Simple example using Jython and JPype 
Categories: Blog