📜  Jython-Java应用程序

📅  最后修改于: 2020-11-08 07:16:45             🧑  作者: Mango


下载jython-standalone-2.7.0.jar-从其官方下载页面上将Jython嵌入Java应用程序中: http : //www.jython.org/downloads.html并将此jar文件包含在Java CLASSPATH环境变量中。

该库包含PythonInterpreter类。使用此类的对象,可以使用execfile()方法执行任何Python脚本。 PythonInterpreter使您可以直接使用PyObject 。 Jython运行时系统已知的所有对象都由类PyObject或其子类之一的实例表示。

PythonInterpreter类具有一些常规使用的方法,下表中对此进行了说明。

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, Classjavaclass

Get the value of a variable in the local namespace. The value will be returned as an instance of the given Java class.

以下代码块是一个Java程序,该程序具有PythonInterpreter对象的嵌入式Jython脚本“ hello.py” .usingexecfile()方法。它还显示了如何使用set()和get()方法设置或读取Python变量。

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 ");
   }
}

编译并运行上述Java程序以获得以下输出。

Hello, world from Java
hello world from Python
42
x: 4
Goodbye