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.
Sr.No. | Method & Description |
---|---|
1 | setIn(PyObject) Set the Python object to use for the standard input stream |
2 | setIn(java.io.Reader) Set a java.io.Reader to use for the standard input stream |
3 | setIn(java.io.InputStream) Set a java.io.InputStream to use for the standard input stream |
4 | setOut(PyObject) Set the Python object to use for the standard output stream |
5 | setOut(java.io.Writer) Set the java.io.Writer to use for the standard output stream |
6 | setOut(java,io.OutputStream) Set the java.io.OutputStream to use for the standard output stream |
7 | setErr(PyObject) Set a Python error object to use for the standard error stream |
8 | setErr(java.io.Writer Set a java.io.Writer to use for the standard error stream |
9 | setErr(java.io.OutputStream) Set a java.io.OutputStream to use for the standard error stream |
10 | eval(String) Evaluate a string as Python source and return the result |
11 | eval(PyObject) Evaluate a Python code object and return the result |
12 | exec(String) Execute a Python source string in the local namespace |
13 | exec(PyObject) Execute a Python code object in the local namespace |
14 | execfile(String filename) Execute a file of Python source in the local namespace |
15 | execfile(java.io.InputStream) Execute an input stream of Python source in the local namespace |
16 | compile(String) Compile a Python source string as an expression or module |
17 | compile(script, filename) Compile a script of Python source as an expression or module |
18 | set(String name, Object value) Set a variable of Object type in the local namespace |
19 | set(String name, PyObject value) Set a variable of PyObject type in the local namespace |
20 | get(String) Get the value of a variable in the local namespace |
21 | get(String name, Class 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