Jython is an implementation of Python 2.7 written in 100% Pure Java, providing seamless integration with the Java platform and ecosystem.
npx @tessl/cli install tessl/maven-org-python--jython-standalone@2.7.0Jython 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.
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.4</version>
</dependency>import org.python.util.PythonInterpreter;
import org.python.core.*;
import org.python.jsr223.PyScriptEngineFactory;
import javax.script.ScriptEngine;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();
}
}Jython's architecture consists of several key components:
org.python.core.*)org.python.util.*)org.python.jsr223.*)Py class)PySystemState)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);
}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[]);
}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();
}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);
}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;
}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);
}// 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();
}