Jython - The language that mixes Java with Python

  5 mins read  

Jython - The language that mixes Java with Python

This language joins two good tools into one. 🐍

Introduction

Jython is an implementation of the Python language (https://terminalroot.com.br/2019/10/script-basico-de-python-para-learned.html) that generates bytecode for Java machines (JVM). With it is possible to develop hybrid applications that unite code in Java and Python.

This tool is also very useful for embedding a scripting language in applications that require this kind of functionality. It also includes a compiler that converts Python source code to Java bytecode, allowing Python programmers to write classes that can be used by a Java program.

Among its advantages are:

  • 🐍 - Development Speed: Python is a Rapid Application Development (RAD) language.
  • 🐍 - Practicality: It is not necessary to compile Jython code to test it. This makes debugging much faster. After the process is finished, the Jythonc compiler is used to compile the program, to include the Jython libraries there is the freeze option.
  • 🐍 - Learning Time: By having a simple, clean and coherent syntax your learning is very easy.

Installation

Procedure on Linux

  • 1. - Install the dependencies First you will need to have it installed on your system:
  • JDK - Java development environment;
  • and IcedTea - An integration software.

Use your distribution’s package manager to install them, for example, as I installed here on Gentoo:

sudo USE="-cups -alsa" emerge -a dev-java/openjdk-bin dev-java/openjdk-jre-bin

In this case I rejected the modules: cups and alsa and icedtea was installed automatically as it is a dependency.

Now download Jython at: https://sourceforge.net/projects/jython/. After downloading extract the package:

Create and enter a directory first so that files are not scattered

mkdir environment && cd environment/
mv ../jython* .
jar xj jython.jar
java -jar jython_installer-*.jar

After this last command, it will open a ** wizard ** for you to follow, follow the images below:

Jython Wizard Jython Wizard Jython Wizard

In this 4th image ↓ part you could still choose a custom directory, for example from ~/.config/jython, it would be the most ideal not to pollute your home directory. Jython Wizard

Jython Wizard Jython Wizard Jython Wizard Jython Wizard Jython Wizard Jython Wizard

After finish we will now edit your vim ~/.bashrc and add the following line:

In this case my version and directory name is 2.5.2, check yours.

PATH="${PATH}:${HOME}/jython2.5.2/bin/"

Then run the source ~/.bashrc command or close and open the terminal so that the jython command can be found.

Creating Your First Application

A mini application that adds two numbers. Create a file named vim sum.py

import javax.swing as libswing
pnumero = libswing.JOptionPane.showInputDialog ("Enter an Integer:")
snumero = libswing.JOptionPane.showInputDialog ("Enter an Integer:")
sum = int (number) + int (number)
libswing.JOptionPane.showMessageDialog (None, "The sum is% d"% sum)

Save and run as follows:

jython sum.py

No Shell will show the outputs of the program, but it does not mean that it is a problem, but it is sending the outputs correctly.

Jython Wizard Jython Wizard

Note that in this case I added 8 + 8 and the result was as expected. 😁️

Explaining the code

  • import javax.swing as libswing - Imports the Java library.
  • pnumero = libswing.JOptionPane.showInputDialog ("Enter an Integer: ") - Shows the first dialog box and stores the value in the pnumero variable.
  • snumero = libswing.JOptionPane.showInputDialog ("Enter an Integer: ") - Displays the second dialog and stores the value in the snumero variable.
  • sum = int (number) + int (number) - Converts the number and number values ​​to integers and adds both by storing the result in the sum variable.
  • libswing.JOptionPane.showMessageDialog (None," The sum is% d "% sum) - Shows the result of the sum on the screen, isn’t it?

Conclusion

I particularly enjoyed Jython 🙌️ and I will dig deeper into it and there will probably be more tutorials here about it. Is that you ?! Liked ?! Tell us using the comment field. Hugs!

Join our Facebook group