Jython - Java Application

Download jython-standalone-2.7.0.jar - For embedding Jython in Java applications from their official downloads page: http://www.jython.org/downloads.html and include this jar file in Java CLASSPATH environment variable.
This library contains the PythonInterpreter class. Using the object of this class, any Python script can be executed using the execfile() method. The PythonInterpreter enables you to make use of PyObjects directly. All objects known to the Jython runtime system are represented by an instance of the class PyObject or one of its subclasses.
The PythonInterpreter class has some regularly used methods, which are explained in the table given below.
setIn(PyObject)
Set the Python object to use for the standard input stream
setIn(java.io.Reader)
Set a java.io.Reader to use for the standard input stream
setIn(java.io.InputStream)
Set a java.io.InputStream to use for the standard input stream
setOut(PyObject)
Set the Python object to use for the standard output stream
setOut(java.io.Writer)
Set the java.io.Writer to use for the standard output stream
setOut(java,io.OutputStream)
Set the java.io.OutputStream to use for the standard output stream
setErr(PyObject)
Set a Python error object to use for the standard error stream
setErr(java.io.Writer
Set a java.io.Writer to use for the standard error stream
setErr(java.io.OutputStream)
Set a java.io.OutputStream to use for the standard error stream
eval(String)
Evaluate a string as Python source and return the result
eval(PyObject)
Evaluate a Python code object and return the result
exec(String)
Execute a Python source string in the local namespace
exec(PyObject)
Execute a Python code object in the local namespace
execfile(String filename)
Execute a file of Python source in the local namespace
execfile(java.io.InputStream)
Execute an input stream of Python source in the local namespace
compile(String)
Compile a Python source string as an expression or module
compile(script, filename)
Compile a script of Python source as an expression or module
set(String name, Object value)
Set a variable of Object type in the local namespace
set(String name, PyObject value)
Set a variable of PyObject type in the local namespace
get(String)
Get the value of a variable in the local namespace
get(String name, Classjavaclass
Get the value of a variable in the local namespace. The value will be returned as an instance of the given Java class.
The following code block is a Java program having an embedded Jython script “hello.py”.usingexecfile() method of the PythonInterpreter object. It also shows how a Python variable can be set or read using set() and get() methods.
import org.python.util.PythonInterpreter;
import org.python.core.*;

public class SimpleEmbedded {
   public static void main(String []args) throws PyException {
      PythonInterpreter interp = new PythonInterpreter();
      System.out.println("Hello, world from Java");
      interp.execfile("hello.py");
      interp.set("a", new PyInteger(42));
      interp.exec("print a");
      interp.exec("x = 2+2");
      PyObject x = interp.get("x");
      System.out.println("x: "+x);
      System.out.println("Goodbye ");
   }
}
Compile and run the above Java program to obtain the following output.
Hello, world from Java
hello world from Python
42
x: 4
Goodbye

 Print
 Add Notes
 Bookmark this page