or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mdexception-handling.mdindex.mdjava-integration.mdjsr223-scripting.mdpython-execution.mdpython-objects.md
tile.json

tessl/maven-org-python--jython-standalone

Jython is an implementation of Python 2.7 written in 100% Pure Java, providing seamless integration with the Java platform and ecosystem.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.python/jython-standalone@2.7.x

To install, run

npx @tessl/cli install tessl/maven-org-python--jython-standalone@2.7.0

index.mddocs/

Jython

Jython is an implementation of Python 2.7 written in 100% Pure Java that provides seamless integration with the Java platform and ecosystem. It enables Python code execution on any Java Virtual Machine while offering access to Java libraries and frameworks directly from Python code. Jython serves as a bridge technology allowing Python developers to leverage Java's extensive ecosystem while maintaining Python's syntax and semantics.

Package Information

  • Package Name: org.python:jython-standalone
  • Package Type: maven
  • Language: Java
  • Installation:
    <dependency>
      <groupId>org.python</groupId>
      <artifactId>jython-standalone</artifactId>
      <version>2.7.4</version>
    </dependency>

Core Imports

import org.python.util.PythonInterpreter;
import org.python.core.*;
import org.python.jsr223.PyScriptEngineFactory;
import javax.script.ScriptEngine;

Basic Usage

import org.python.util.PythonInterpreter;
import org.python.core.*;

public class JythonExample {
    public static void main(String[] args) {
        // Initialize Jython
        PythonInterpreter.initialize(System.getProperties(), 
                                   System.getProperties(), 
                                   new String[0]);
        
        // Create interpreter
        PythonInterpreter interp = new PythonInterpreter();
        
        // Execute Python code
        interp.exec("print('Hello from Python!')");
        
        // Set variables from Java
        interp.set("x", 42);
        interp.exec("y = x * 2");
        
        // Get results back to Java
        PyObject result = interp.get("y");
        System.out.println("Result: " + result.asInt());
        
        interp.close();
    }
}

Architecture

Jython's architecture consists of several key components:

  • Core Runtime: Python object system implemented in Java (org.python.core.*)
  • Embedding API: High-level interfaces for embedding Python in Java applications (org.python.util.*)
  • JSR-223 Support: Standard Java scripting API compliance (org.python.jsr223.*)
  • Type System: Complete Python type hierarchy with Java integration
  • Object Factory: Centralized creation and conversion utilities (Py class)
  • System State: Python environment and module management (PySystemState)

Capabilities

Python Code Execution

Execute Python code directly from Java applications using multiple approaches: direct interpreter, interactive console, or JSR-223 scripting engine.

public class PythonInterpreter implements AutoCloseable, Closeable {
    // Constructors
    public PythonInterpreter();
    public PythonInterpreter(PyObject dict);
    public PythonInterpreter(PyObject dict, PySystemState systemState);
    public static PythonInterpreter threadLocalStateInterpreter(PyObject dict);
    
    // Code execution
    public void exec(String code);
    public void exec(PyObject code);
    public PyObject eval(String code);
    public PyObject eval(PyObject code);
    public void execfile(String filename);
    public void execfile(java.io.InputStream s);
    public void execfile(java.io.InputStream s, String name);
    
    // Variable management
    public void set(String name, Object value);
    public void set(String name, PyObject value);
    public PyObject get(String name);
    public <T> T get(String name, Class<T> javaclass);
    
    // I/O management
    public void setIn(PyObject inStream);
    public void setIn(java.io.Reader inStream);
    public void setIn(java.io.InputStream inStream);
    public void setOut(PyObject outStream);
    public void setOut(java.io.Writer outStream);
    public void setOut(java.io.OutputStream outStream);
    public void setErr(PyObject outStream);
    public void setErr(java.io.Writer outStream);
    public void setErr(java.io.OutputStream outStream);
    
    // Namespace management
    public PyObject getLocals();
    public void setLocals(PyObject d);
    public PyObject getGlobals();
    public PySystemState getSystemState();
    
    // Code compilation
    public PyCode compile(String script);
    public PyCode compile(Reader reader);
    public PyCode compile(String script, String filename);
    public PyCode compile(Reader reader, String filename);
    
    // Resource management
    public void close();
    public void cleanup();
    
    // Initialization
    public static void initialize(Properties preProperties, Properties postProperties, String[] argv);
}

Python Code Execution

Python Object Creation and Manipulation

Create, manipulate, and convert Python objects from Java using the comprehensive factory methods and object hierarchy.

public final class Py {
    // Object creation
    public static PyInteger newInteger(int i);
    public static PyObject newInteger(long i);
    public static PyLong newLong(String s);
    public static PyLong newLong(java.math.BigInteger i);
    public static PyLong newLong(int i);
    public static PyLong newLong(long l);
    public static PyFloat newFloat(float v);
    public static PyFloat newFloat(double v);
    public static PyString newString(char c);
    public static PyString newString(String s);
    public static PyUnicode newUnicode(char c);
    public static PyUnicode newUnicode(String s);
    public static PyBoolean newBoolean(boolean t);
    public static PyStringMap newStringMap();
    
    // Type conversion
    public static PyObject java2py(Object o);
    public static PyString java2py(String s);
    public static PyInteger java2py(int i);
    public static PyInteger java2py(long l);
    public static PyFloat java2py(float f);
    public static PyFloat java2py(double d);
    public static PyBoolean java2py(boolean b);
}

public class PyObject implements Serializable {
    public PyObject __call__(PyObject args[]);
    public PyObject __getattr__(String name);
    public void __setattr__(String name, PyObject value);
    public Object __tojava__(Class<?> c);
}

// Collection constructors (no factory methods available)
public class PyList extends PySequenceList {
    public PyList();
    public PyList(PyObject[] elements);
    public PyList(Collection<?> c);
}

public class PyDictionary extends PyObject {
    public PyDictionary();
    public PyDictionary(Map<PyObject, PyObject> map);
    public PyDictionary(PyObject elements[]);
}

Python Objects

Java-Python Integration

Bridge between Java and Python types with automatic conversion, method invocation, and exception handling.

public class PyObject implements Serializable {
    public Object __tojava__(Class<?> c);
    public String toString();
    public boolean equals(Object o);
    public int hashCode();
}

// Built-in Python types
public class PyString extends PyBaseString {
    public String getString();
    public PyString(String string);
}

public class PyList extends PySequenceList {
    public void append(PyObject o);
    public PyObject pop();
    public int size();
}

Java-Python Integration

JSR-223 Scripting Engine

Use Jython through the standard Java scripting API for seamless integration with existing Java applications that support JSR-223.

public class PyScriptEngineFactory implements ScriptEngineFactory {
    public String getEngineName();
    public String getLanguageVersion();
    public ScriptEngine getScriptEngine();
}

public class PyScriptEngine extends AbstractScriptEngine 
                           implements Compilable, Invocable, AutoCloseable {
    public Object eval(String script, ScriptContext context);
    public CompiledScript compile(String script);
    public Object invokeFunction(String name, Object... args);
}

JSR-223 Scripting

System Configuration and Options

Configure Jython runtime behavior, system state, Python path, and various performance and debugging options.

public class PySystemState extends PyObject {
    public static void initialize(Properties preProperties, Properties postProperties, String[] argv);
    public static PySystemState getDefault();
    public PyList path;          // Python module search path
    public PyList argv;          // Command line arguments
    public PyDictionary modules; // Loaded modules cache
}

public class Options {
    public static boolean showJavaExceptions;
    public static boolean respectJavaAccessibility;
    public static int verbose;
    public static boolean debug;
}

Configuration

Exception Handling

Handle Python exceptions in Java code with comprehensive exception types and conversion utilities.

public class PyException extends RuntimeException {
    public PyObject type;        // Exception type
    public PyObject value;       // Exception value  
    public PyTraceback traceback; // Python traceback
    
    public PyException(PyObject type, String value);
    public boolean match(PyObject exc);
    public void normalize();
}

// Exception factory methods in Py class
public final class Py {
    public static PyException TypeError(String message);
    public static PyException ValueError(String message);
    public static PyException RuntimeError(String message);
    public static PyException AttributeError(String message);
}

Exception Handling

Types

// Base class for all Python objects
public class PyObject implements Serializable {
    public PyType getType();
    public boolean isCallable();
    public boolean isSequenceType();
    public boolean isMappingType();
    public boolean isNumberType();
    
    // Python protocol methods
    public PyObject __add__(PyObject other);
    public PyObject __getitem__(PyObject key);
    public PyObject __iter__();
    public String __str__();
    public String __repr__();
}

// System state management
public class PySystemState extends PyObject {
    public PyList path;
    public PyList argv;
    public PyDictionary modules;
    public PyObject stdin;
    public PyObject stdout;
    public PyObject stderr;
}

// Main embedding interface
public class PythonInterpreter implements AutoCloseable, Closeable {
    public void set(String name, Object value);
    public PyObject get(String name);
    public PyObject getLocals();
    public PyObject getGlobals();
    public PySystemState getSystemState();
}