zoukankan      html  css  js  c++  java
  • jython embedded into java

    http://mavenhub.com/mvn/central-sync/org.python/jython-standalone/2.5.2

    http://wiki.python.org/jython/UserGuide#embedding-jython

    Embedding Jython

    There are two options for embedding Jython in a Java application. You can make a real Java class out of a Python class, and then call it from your Java code, as previously described, or you can use the PythonInterpreter object

    Information on the PythonInterpreter can be found in the JavaDoc documentation for org.python.util.PythonInterpreter.

    The following example demonstrates how to use the PythonInterpreter to execute a simple Python program.

    The python program:

    import sys
    print sys
    a = 42
    print a
    x = 2 + 2
    print "x:",x

    The java code required to execute the python program:

    import org.python.core.PyException;
    import org.python.core.PyInteger;
    import org.python.core.PyObject;
    import org.python.util.PythonInterpreter;
    
    public class SimpleEmbedded {
    
        public static void main(String[] args) throws PyException {
            PythonInterpreter interp = new PythonInterpreter();
            interp.exec("import sys");
            interp.exec("print sys");
            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);
        }
    }

    Note that the term "PythonInterpreter" does not mean the Python code is interpreted; in all cases, Python programs in Jython are compiled to Java bytecode before execution, even when run from the command line or through the use of methods like exec.

  • 相关阅读:
    php stdClass转数组
    PHP 获取1970年前的时间戳,且为负
    springboot响应格式Resullt封装
    php使用elasticsearch
    day0620211207
    day0820211209
    day022021121
    day0520211206
    day0720211208
    day0320211202
  • 原文地址:https://www.cnblogs.com/lexus/p/2356444.html
Copyright © 2011-2022 走看看