Introduction to Jython

The Jython project provides implementations of Python that run on the Java Virtual Machine, giving Python developers access to the vast ecosystem of Java libraries while maintaining Python's elegant syntax and productivity advantages.

Current Status

The current stable release (Jython 2.7.x) supports Python 2 syntax. Development towards Python 3 compatibility is actively ongoing in the project's GitHub repository.

Jython implementations are freely available for both commercial and non-commercial use, distributed with source code under the PSF License v2. This makes Jython an excellent choice for enterprise environments and open-source projects alike.

Key Benefits

🔧 Embedded Scripting

Java programmers can integrate Jython libraries to allow end users to write scripts that extend application functionality, from simple automation to complex business logic.

⚡ Interactive Development

Jython provides an interactive interpreter for experimenting with Java packages and debugging running applications in real-time.

🚀 Rapid Prototyping

Python programs are typically 2-10x shorter than equivalent Java code, dramatically increasing developer productivity and reducing time-to-market.

Who Uses Jython?

Jython is embedded in numerous enterprise and open-source projects across various industries. Here are some notable implementations:

IBM WebSphere

Uses Jython to provide powerful administrative scripting capabilities for enterprise application server management.

Apache Pig

Leverages Jython to support user-defined functions (UDFs) for big data processing and analytics workflows.

JEM/JythonMusic

An innovative environment for music composition and creative programming using Jython's expressive capabilities.

Many organizations choose Jython for its ability to bridge Python's ease of use with Java's enterprise-grade performance and security features. This makes it particularly valuable in:

  • Financial Services: Risk analysis and algorithmic trading systems
  • Scientific Computing: Data analysis and visualization tools
  • Web Applications: Rapid development of web services and APIs
  • DevOps & Automation: Build systems and deployment pipelines
Installation Guide

Jython 2.7.2 is distributed via an executable JAR file installer that works on any system with Java installed.

Download and Install

After downloading the installer, you have two options:

GUI Installation (recommended)
# Double-click the installer file, or run: java -jar jython-installer-2.7.2.jar
Console Installation (headless systems)
# For servers without GUI java -jar jython-installer-2.7.2.jar --console

System Requirements

  • Java 8 or higher (OpenJDK or Oracle JDK)
  • Minimum 512MB RAM (2GB+ recommended for development)
  • 50MB disk space for basic installation

Verification

After installation, verify Jython is working correctly:

# Test the installation jython --version # Start interactive interpreter jython
Getting Started

Let's create your first Jython program! This example demonstrates how to embed Python code within a Java application.

Hello World Example

JythonHelloWorld.java
import org.python.util.PythonInterpreter; public class JythonHelloWorld { public static void main(String[] args) { try (PythonInterpreter pyInterp = new PythonInterpreter()) { // Execute Python code from Java pyInterp.exec("print('Hello from Jython!')"); // You can also run more complex Python code pyInterp.exec(""" for i in range(3): print(f'Message {i + 1}: Welcome to Jython!') """); } } }

Compilation and Execution

# Compile the Java class javac -cp jython.jar JythonHelloWorld.java # Run the program java -cp .:jython.jar JythonHelloWorld

Expected Output

Hello from Jython!
Message 1: Welcome to Jython!
Message 2: Welcome to Jython!
Message 3: Welcome to Jython!
Advanced Examples

Working with Java Objects

One of Jython's most powerful features is seamless Java integration:

JavaIntegration.java
import org.python.util.PythonInterpreter; import java.util.ArrayList; public class JavaIntegration { public static void main(String[] args) { try (PythonInterpreter pyInterp = new PythonInterpreter()) { // Create Java object and pass to Python ArrayList<String> javaList = new ArrayList<>(); pyInterp.set("java_list", javaList); // Manipulate Java object from Python pyInterp.exec(""" import java.util.Date as Date # Add items using Python syntax java_list.add('Hello') java_list.add('from') java_list.add('Python!') # Create Java objects from Python current_time = Date() java_list.add(str(current_time)) print('List contents:') for item in java_list: print(f' - {item}') """); } } }

File Processing Example

FileProcessor.py (Pure Jython Script)
# This script can access both Python and Java libraries import java.io.File as File import java.io.FileWriter as FileWriter import java.io.BufferedReader as BufferedReader import java.io.FileReader as FileReader def process_file(input_path, output_path): """Process a text file using Java I/O from Python""" try: # Read file using Java classes file_reader = BufferedReader(FileReader(input_path)) lines = [] line = file_reader.readLine() while line is not None: lines.append(line.upper()) # Process with Python line = file_reader.readLine() file