Image Blog What is Jython
August 13, 2019

What Is Jython?

Java Application Development
Enterprise Development

Jython is a version of the Python programming language that runs on the Java platform. It allows users to write programs in Python and compile them to Java bytecodes that run directly on a Java Virtual Machine, or JVM. It’s similar to otherJVM languages like; Scala, Kotlin, Groovy, or Clojur.

In this article, we'll dive into the benefits of Jython, compare Jython vs Python, and give links to additional resources for learning about this hybrid programming language.

Back to top

Key Benefits of Jython

While Jython has many benefits for developers, there are four key benefits that draw users to this JVM programming language;

  1. Reusability of Java features: The usability of Java classes and interfaces in your Jython code is one of the best deals available. Also, other features which make Java stand apart from the crowd, such as multithreading and automatic garbage collection, enhance the feature set even more.
  2. Java bytecode compilation: One of the biggest strengths of Java that amazed the programming fraternity when it was introduced was the concept of bytecode generation in the form of a *.class file — a sweet piece of undecipherable text generated for every class and interface present inside your Java file. Apart from lending a hand in making the language portable across various platforms, it also improves performance as well.
  3. Easy frame creation: As noted in the example pictured below, Jython simplifies the frame creation process.
  4. High Level Language Accessibility: Jython gives you a great combination of HLL (high level language) and the most popular scripting language. It’s great if you need a to implement a quick GUI, or if you just need a small piece of code, or a method to initiate a call to a database.
Back to top

About Python

Python has been the most popular programming language in the world for the last two years according to the IEEE’s Spectrum magazine ranking. C, in first place only a couple of years ago, has moved into third place, overtaken by Java in second, and C++ is in fourth place.

IEEE Spectrum magazine editors offer reasons to explain why Python continues to gain programmer mindshare. Importantly, they now list it as an embedded language — because the newest generations of microcontrollers can mitigate Python’s relatively poor performance as an interpreted language. Another big reason in the cutting-edge, research-oriented IEEE community is that, in many organizations, it has supplanted the R language, which had been the leader in data science research and big data applications.

Python, in general, continues to grow in popularity because it is easier to learn and use. Because of this, in addition to popularity among professional developers, Python has been ranked as the first language taught in most universities for some time now. The benefits aren’t just for beginners: for professionals, it can drive productivity improvements.

Back to top

Jython vs. Python

In a head to head comparison of Jython vs Python, Jython offers all that Python offers and almost all that Java does as well -- like database connectivity, cryptographic features, and GUI among a lot of others. The developer may write Jython components and modules which can later be integrated with/into a Java application, if desired.

Java has some characteristics which Python does not (extensive libraries for various purposes and built-in support for many functionalities) and Python has some which Java doesn’t (an easily-understandable syntax to begin with, scalability). Considering the extensive feature sets of both the languages, it is prudent to think what can be achieved by their amalgamation: a lot of features and syntactical simplicity, concurrently. After all, what is Jython but a combination of Java and Python?

Jython programs can use any Java class. Except for some standard modules, these programs use Java classes instead of Python modules. Jython also includes almost all of the modules in the standard Python programming language distribution.

Consider this simple piece of Jython code, which displays a frame with a title:


>>> import javax.swing as swing
>>> f = swing.JFrame(title = "My First Jython Frame", visible = 1, size = (400, 400))


Here is a Java implementation that has the same result.


import javax.swing.JFrame;
public class MyFrame extends JFrame {
	public MyFrame(String title) {
		this.setTitle(title);
		this.setSize(400, 400);
		this.setResizable(false);
	}
public static void main(String[] args) {
		new MyFrame("My First Java Frame").setVisible(true);
	}
}
Back to top

What Is Jython Used For?

Because of Python’s incredible popularity, fans want to use it everywhere they can. This includes in environments running Java. Jython should work wherever the JVM can function. I’ve even read suggestions that it could work on the Android operating system, but this may fall into the should work category.

IBM appears to be a big advocate of Jython, most likely because they have great investments in Java. The company has many users internally and at customers who are among those driving Python popularity. These users gravitate toward it instead of using the language R, and other languages like Scala because of its greater suitability in building tools to solve scientific problems in data science, machine learning, and deep learning. The strengths of Python lie in its ability to employ all of these problem-solving approaches.

The magic of Jython in this arena, is it gives developers and scientists access to Python along with Java classes so they can build special-purpose programs in a much simpler language, that opens their work up to more collaborators. With the combination of Java and Python, developers can escape single-threaded Python and get better performance.

Back to top

Final Thoughts

One caveat is that Jython may be stuck on Python 2.7 (at least for the moment), whereas Python 3 has come into widespread use. There is hope, however, in that the the Github mirror of the Python.org code home for Jython continues to be updated, and even has support for Java 12. So don’t be afraid to experiment with Jython, and since you’ll likely be writing your own code, you may not have to worry too much about the compatibility with the latest language features while obtaining the benefits we’ve described.

Additional Resources

You can find out more about Jython at www.jython org. There’s even an online book, called Jython Book (appropriately) that describes in great detail how to use it. It even describes how to use it with the Eclipse IDE.

Search GitHub, and you’ll find a number of repos with useful code for integrating Jython into your own work. For example, I found a framework to simplify WebSphere Application Server scripting, called WDR. Another notable contribution was an interface so you can use Jython to write extensions for the popular Burp security suite, if you prefer using Python versus its own built-in language.

Want to Save Time During Java Development?

Try JRebel! It eliminates redeploys to speed up the coding process, allowing for real-time visibility into code changes.

Try JRebel for Free

Back to top