Embedding Jython in Java

What follows is an example of embedding Jython in the Java code that ships with Jython's standard distribution. I've added comments.

 import org.python.util.PythonInterpreter; import org.python.core.*; public class SimpleEmbedded { public static void main(String []args) throws PyException { // Create a Python interpreter. PythonInterpreter interp = new PythonInterpreter(); System.out.println("Hello, brave new world"); // Execute an import statement and a // print statement. interp.exec("import sys"); interp.exec("print sys"); // Create a variable by assigning // it the value 42. // PyInterger is a Python integer. interp.set("a", new PyInteger(42)); // Execute the print statement to // print out the value of a. interp.exec("print ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.