Introduction

Python is one of the world's most popular programming languages because of its simple syntax, readability, and huge ecosystem. But did you know you can also run Python code directly on the Java Virtual Machine (JVM)?

That's exactly what Jython does.

Jython allows developers to write Python code while accessing Java libraries and Java applications without needing special wrappers.

In this comprehensive tutorial, you'll learn:

  • What Jython is
  • Features of Jython
  • Installing Jython
  • Running Jython programs
  • Variables and data types
  • Control flow
  • Functions
  • Object-oriented programming
  • Importing Java classes
  • File handling
  • Exception handling
  • Modules
  • Best practices
  • Advantages and disadvantages
  • Frequently Asked Questions

Whether you're a Python beginner or a Java developer, this guide will help you understand how Jython works.


What is Jython?

Jython is an implementation of the Python programming language that runs on the Java Virtual Machine (JVM).

Unlike CPython (the standard Python implementation), Jython compiles Python code into Java bytecode.

This allows Python programs to interact directly with Java classes and libraries.

Example:

Python Code

Jython Compiler

Java Bytecode

Java Virtual Machine

Key Features of Jython

✔ Runs on the Java Virtual Machine

✔ Uses Java libraries directly

✔ Supports Python syntax

✔ Platform independent

✔ Excellent for enterprise applications

✔ Integrates with existing Java software

✔ Easy scripting inside Java applications


Jython vs CPython

FeatureJythonCPython
Runs on JVMYesNo
Supports Java LibrariesYesNo
Supports C ExtensionsNoYes
PerformanceGoodExcellent
PlatformJVMNative
Uses Java ClassesYesNo

Installing Jython

Step 1

Download the latest Jython installer from the official website.

Step 2

Install Java if it is not already installed.

Verify Java:

java -version

Example:

java version "17.0.2"

Step 3

Install Jython.

Verify installation:

jython --version

Output:

Jython 2.7.x

Your First Jython Program

Create a file:

print("Hello Jython!")

Run:

jython hello.py

Output:

Hello Jython!

Variables

name = "Alice"

age = 25

height = 5.7

student = True

print(name)
print(age)

Output

Alice
25

Data Types

number = 100

price = 19.99

text = "Python"

flag = False

colors = ["Red","Blue","Green"]

person = {
"name":"John",
"age":30
}

Common data types

  • Integer
  • Float
  • String
  • Boolean
  • List
  • Dictionary
  • Tuple
  • Set

User Input

name = raw_input("Enter your name: ")

print("Hello", name)

Operators

Arithmetic

a = 20
b = 5

print(a+b)
print(a-b)
print(a*b)
print(a/b)

Comparison

print(a>b)
print(a==b)
print(a!=b)

Logical

x = True
y = False

print(x and y)
print(x or y)
print(not x)

If Statements

age = 18

if age >= 18:
print("Adult")
else:
print("Minor")

Nested If

score = 88

if score >= 80:
if score >= 90:
print("A")
else:
print("B")

For Loop

for i in range(5):
print(i)

Output

0
1
2
3
4

While Loop

count = 1

while count <= 5:
print(count)
count += 1

Functions

def greet(name):
print("Hello", name)

greet("David")

Output

Hello David

Return Values

def square(x):
return x*x

print(square(8))

Output

64

Lists

fruits = ["Apple","Banana","Orange"]

print(fruits[0])

fruits.append("Mango")

print(fruits)

Tuples

point = (10,20)

print(point[0])

Dictionaries

student = {
"name":"Tom",
"age":20
}

print(student["name"])

Classes

class Person(object):

def __init__(self,name):
self.name = name

def hello(self):
print("Hello",self.name)

p = Person("John")

p.hello()

Exception Handling

try:
number = 10/0
except ZeroDivisionError:
print("Cannot divide by zero")

File Handling

Write

file = open("data.txt","w")

file.write("Hello")

file.close()

Read

file = open("data.txt")

print(file.read())

file.close()

Importing Java Classes

One of the biggest advantages of Jython is direct access to Java.

Example:

from java.util import Date

today = Date()

print(today)

Java ArrayList Example

from java.util import ArrayList

items = ArrayList()

items.add("Apple")
items.add("Banana")

print(items)

Java Random Example

from java.util import Random

random = Random()

print(random.nextInt(100))

Java Swing GUI Example

from javax.swing import JFrame

frame = JFrame("Jython Window")

frame.setSize(400,300)

frame.setVisible(True)

Creating Modules

math_tools.py

def add(a,b):
return a+b

main.py

import math_tools

print(math_tools.add(5,7))

Best Practices

  • Use meaningful variable names.
  • Organize code into modules.
  • Follow Python naming conventions.
  • Handle exceptions properly.
  • Reuse Java libraries instead of rewriting functionality.
  • Keep functions small and focused.
  • Write comments only when they add clarity.
  • Test your code regularly.

Advantages of Jython

  • Seamless Java integration
  • Familiar Python syntax
  • Platform independent
  • Easy deployment in Java environments
  • Access to thousands of Java libraries
  • Great for enterprise applications
  • Simplifies scripting for Java software

Limitations of Jython

  • Based on Python 2.7 syntax and does not support modern Python 3 features.
  • Does not support most C extension modules such as NumPy and many scientific packages.
  • Smaller community than standard Python.
  • Slower release cycle.
  • Less suitable for modern machine learning or data science projects.

When Should You Use Jython?

Jython is a good choice if you:

  • Need to automate Java applications.
  • Want to extend enterprise Java software with Python scripts.
  • Work in environments where the JVM is the standard runtime.
  • Need direct access to Java APIs from Python-like code.

For new Python projects that rely on modern Python 3 features, the standard CPython implementation is generally the better option.


Frequently Asked Questions (FAQ)

Is Jython the same as Python?

No. Jython is an implementation of Python that runs on the Java Virtual Machine, while the standard Python implementation (CPython) runs natively.

Does Jython support Python 3?

No. The latest stable Jython release is based on Python 2.7 syntax.

Can Jython use Java libraries?

Yes. Jython can directly import and use Java classes without special wrappers.

Can Jython run NumPy?

Generally no. Most packages that depend on CPython C extensions, including NumPy, are not supported.

Is Jython free?

Yes. Jython is free and open source.


Conclusion

Jython bridges the gap between Python and Java, allowing developers to write clean Python-style code while leveraging the extensive Java ecosystem. It is particularly useful for enterprise environments, scripting Java applications, and integrating with existing JVM-based systems.

Although Jython does not support modern Python 3 features or many native C extension libraries, it remains a valuable tool for projects that require seamless Java interoperability. Understanding Jython expands your programming toolkit and helps you choose the right implementation for the right job.

Jython Tutorial: Complete Beginner's Guide to Python on the Java Virtual Machine (JVM)