Jython is an implementation of Python 2.7 written in 100% Pure Java, providing seamless integration with the Java platform and ecosystem.
—
Jython provides extensive configuration options to customize runtime behavior, system state, module loading, and performance characteristics.
Manages the global Python system state, equivalent to Python's sys module.
public class PySystemState extends PyObject {
// Initialization
public static void initialize();
public static void initialize(Properties preProperties, Properties postProperties);
public static void initialize(Properties preProperties, Properties postProperties, String[] argv);
public static PySystemState getDefault();
// System attributes
public PyList path; // Module search path (sys.path)
public PyList argv; // Command line arguments (sys.argv)
public PyDictionary modules; // Loaded modules cache (sys.modules)
public PyObject stdin; // Standard input stream
public PyObject stdout; // Standard output stream
public PyObject stderr; // Standard error stream
public PyString version; // Python version string
public PyTuple version_info; // Version information tuple
public PyString platform; // Platform identifier
public PyString prefix; // Installation prefix
public PyString exec_prefix; // Executable prefix
// Path management
public static void add_package(String n);
public static void add_extdir(File file);
public static void add_classdir(File file);
// Module management
public PyObject getBuiltins();
public void setBuiltins(PyObject builtins);
// Cleanup
public void close();
}Global configuration options that affect Jython runtime behavior.
public class Options {
// Exception handling
public static boolean showJavaExceptions = false;
public static boolean includeJavaStackInExceptions = true;
public static boolean showPythonProxyExceptions = false;
// Security and access control
public static boolean respectJavaAccessibility = true;
// Module importing and site packages
public static boolean importSite = true;
public static boolean no_site = false;
// Interactive mode and inspection
public static boolean interactive = false;
public static boolean inspect = false;
// Case sensitivity and imports
public static boolean caseok = false;
// Verbosity and debugging
public static int verbose = 0;
public static String proxyDebugDirectory;
// Python 3 compatibility and warnings
public static boolean Qnew = false;
public static boolean py3k_warning = false;
public static int bytes_warning = 0;
public static int division_warning = 0;
// I/O and environment
public static boolean unbuffered = false;
public static boolean ignore_environment = false;
public static boolean no_user_site = false;
// Compilation and optimization
public static boolean dont_write_bytecode = false;
public static int optimize = 0;
// Regular expression caching
public static String sreCacheSpec;
}import org.python.core.*;
import org.python.util.PythonInterpreter;
import java.util.Properties;
public class JythonConfig {
public static void main(String[] args) {
// Configure system properties before initialization
Properties preProps = new Properties();
preProps.setProperty("python.home", "/path/to/jython");
preProps.setProperty("python.executable", "/path/to/jython/bin/jython");
Properties postProps = new Properties();
postProps.setProperty("python.path", "/path/to/modules:/another/path");
// Initialize with custom properties and arguments
String[] jythonArgs = {"--verbose", "script.py"};
PythonInterpreter.initialize(preProps, postProps, jythonArgs);
// Now create interpreter with configured system
PythonInterpreter interp = new PythonInterpreter();
// Use interpreter...
interp.close();
}
}// Configure before creating any interpreters
Options.showJavaExceptions = true; // Show Java stack traces
Options.includeJavaStackInExceptions = false; // Don't include Java traces in Python exceptions
Options.verbose = 2; // High verbosity
Options.debug = true; // Enable debug mode
Options.respectJavaAccessibility = false; // Allow access to private Java members
PythonInterpreter interp = new PythonInterpreter();
// These options are now in effect
interp.exec("import sys; print('Verbose level:', sys.flags.verbose)");
interp.close();PySystemState sys = PySystemState.getDefault();
// Add directories to Python path
PyString customPath = Py.newString("/custom/python/modules");
sys.path.append(customPath);
// Add Java packages to be accessible from Python
PySystemState.add_package("com.mycompany.mypackage");
// Add JAR files to classpath
PySystemState.add_classdir(new File("/path/to/my-library.jar"));
// Print current path
PythonInterpreter interp = new PythonInterpreter();
interp.exec("""
import sys
print("Python path:")
for p in sys.path:
print(" ", p)
""");
interp.close();import java.io.*;
PythonInterpreter interp = new PythonInterpreter();
// Redirect stdout to a string buffer
StringWriter output = new StringWriter();
interp.setOut(output);
// Redirect stderr to a file
FileWriter errorLog = new FileWriter("errors.log");
interp.setErr(errorLog);
// Redirect stdin from a string
StringReader input = new StringReader("Alice\n25\n");
interp.setIn(input);
// Execute Python code that uses I/O
interp.exec("""
name = input("Enter name: ")
age = int(input("Enter age: "))
print(f"Hello, {name}! You are {age} years old.")
import sys
print("This goes to stderr", file=sys.stderr)
""");
// Get captured output
String capturedOutput = output.toString();
System.out.println("Captured: " + capturedOutput);
// Close resources
errorLog.close();
interp.close();// Create custom Python stream objects
PyObject customOut = new PyFile(new FileOutputStream("custom.out"));
PyObject customErr = new PyFile(new FileOutputStream("custom.err"));
interp.setOut(customOut);
interp.setErr(customErr);
interp.exec("""
print("This goes to custom.out")
import sys
print("This goes to custom.err", file=sys.stderr)
""");
interp.close();// Add custom module directories before initialization
Properties props = new Properties();
props.setProperty("python.path",
"/custom/modules:/shared/python:/project/src");
PythonInterpreter.initialize(System.getProperties(), props, new String[0]);
PythonInterpreter interp = new PythonInterpreter();
// Now can import from custom locations
interp.exec("""
# These modules are found in custom paths
import my_custom_module
from shared_utils import helper_function
""");
interp.close();// Disable automatic site package loading
Options.importSite = false;
Options.no_site = true;
PythonInterpreter interp = new PythonInterpreter();
// Manually control what gets imported
interp.exec("""
# Site packages are not automatically loaded
# Need to explicitly add paths if needed
import sys
sys.path.append('/explicit/path/to/packages')
""");
interp.close();// Restrict Java access from Python
Options.respectJavaAccessibility = true; // Respect Java private/protected
PythonInterpreter interp = new PythonInterpreter();
// This configuration affects what Java members are accessible
interp.exec("""
from java.lang import String
s = String("test")
# This will work - public method
print(s.length())
# This might fail depending on respectJavaAccessibility setting
# if trying to access private fields
""");
interp.close();// Control how proxy exceptions are shown
Options.showPythonProxyExceptions = true;
PythonInterpreter interp = new PythonInterpreter();
// Define Python class that implements Java interface
interp.exec("""
class PythonRunnable:
def run(self):
raise ValueError("Python error in Java interface")
runnable = PythonRunnable()
""");
// Get the Python object as Java interface
Runnable runnable = (Runnable) interp.get("runnable").__tojava__(Runnable.class);
try {
runnable.run(); // This will show Python exception details
} catch (Exception e) {
System.out.println("Caught: " + e.getMessage());
}
interp.close();// Enable optimizations before initialization
Options.optimize = true; // Enable optimization
Options.dont_write_bytecode = true; // Don't write .pyc files
PythonInterpreter interp = new PythonInterpreter();
// Check optimization level
interp.exec("""
import sys
print("Optimization level:", sys.flags.optimize)
print("Don't write bytecode:", sys.dont_write_bytecode)
""");
interp.close();// Configure bytecode caching
Options.python_cachedir_skip = ".*test.*"; // Skip caching for test files
// Set custom cache directory via system property
System.setProperty("python.cachedir", "/tmp/jython_cache");
PythonInterpreter interp = new PythonInterpreter();
interp.exec("""
import sys
print("Cache directory:", getattr(sys, 'cachedir', 'default'))
""");
interp.close();// Set different verbosity levels
Options.verbose = 3; // Maximum verbosity
PythonInterpreter interp = new PythonInterpreter();
// Verbose output will show:
// - Module import information
// - Compilation details
// - Runtime debugging info
interp.exec("import json"); // Will show verbose import information
interp.close();Options.debug = true;
PythonInterpreter interp = new PythonInterpreter();
interp.exec("""
import sys
print("Debug mode:", sys.flags.debug)
# Debug mode affects various runtime behaviors
# like exception handling and optimization
""");
interp.close();public class JythonConfigManager {
private static final boolean IS_DEVELOPMENT =
"development".equals(System.getProperty("environment"));
public static void configureForEnvironment() {
if (IS_DEVELOPMENT) {
// Development settings
Options.showJavaExceptions = true;
Options.verbose = 2;
Options.debug = true;
Options.dont_write_bytecode = true;
} else {
// Production settings
Options.showJavaExceptions = false;
Options.verbose = 0;
Options.debug = false;
Options.optimize = true;
}
}
public static PythonInterpreter createConfiguredInterpreter() {
configureForEnvironment();
Properties props = new Properties();
if (IS_DEVELOPMENT) {
props.setProperty("python.path", "src:test:lib");
} else {
props.setProperty("python.path", "lib");
}
PythonInterpreter.initialize(System.getProperties(), props, new String[0]);
return new PythonInterpreter();
}
}public class PropertyBasedConfig {
public static void configure(String propertiesFile) throws IOException {
Properties config = new Properties();
config.load(new FileInputStream(propertiesFile));
// Map properties to Options
Options.showJavaExceptions = Boolean.parseBoolean(
config.getProperty("jython.showJavaExceptions", "false"));
Options.verbose = Integer.parseInt(
config.getProperty("jython.verbose", "0"));
Options.debug = Boolean.parseBoolean(
config.getProperty("jython.debug", "false"));
// System properties for initialization
Properties sysProps = new Properties();
sysProps.setProperty("python.home",
config.getProperty("python.home", ""));
sysProps.setProperty("python.path",
config.getProperty("python.path", ""));
PythonInterpreter.initialize(System.getProperties(), sysProps, new String[0]);
}
}
// Usage:
// PropertyBasedConfig.configure("jython.properties");public class ConfigValidator {
public static void validateConfiguration() {
PythonInterpreter interp = new PythonInterpreter();
// Check system state
PySystemState sys = interp.getSystemState();
System.out.println("=== Jython Configuration ===");
System.out.println("Python version: " + sys.version);
System.out.println("Platform: " + sys.platform);
System.out.println("Python home: " + System.getProperty("python.home", "not set"));
// Check path configuration
System.out.println("\nPython path:");
for (int i = 0; i < sys.path.__len__(); i++) {
System.out.println(" " + sys.path.__getitem__(i));
}
// Check options
System.out.println("\nRuntime options:");
System.out.println(" Show Java exceptions: " + Options.showJavaExceptions);
System.out.println(" Verbose level: " + Options.verbose);
System.out.println(" Debug mode: " + Options.debug);
System.out.println(" Respect Java accessibility: " + Options.respectJavaAccessibility);
interp.close();
}
}public class ThreadLocalJython {
private static final ThreadLocal<PythonInterpreter> THREAD_INTERPRETER =
ThreadLocal.withInitial(() -> {
// Each thread gets its own interpreter with custom config
Properties props = new Properties();
props.setProperty("python.path", "thread-local-modules");
return PythonInterpreter.threadLocalStateInterpreter(
Py.newStringMap() // Custom local namespace
);
});
public static PythonInterpreter getInterpreter() {
return THREAD_INTERPRETER.get();
}
public static void cleanup() {
PythonInterpreter interp = THREAD_INTERPRETER.get();
if (interp != null) {
interp.close();
THREAD_INTERPRETER.remove();
}
}
}
// Usage in multi-threaded environment:
// PythonInterpreter interp = ThreadLocalJython.getInterpreter();
// interp.exec("print('Thread-specific Python execution')");
// ThreadLocalJython.cleanup(); // When thread is doneInstall with Tessl CLI
npx tessl i tessl/maven-org-python--jython-standalone