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.

  • 相关阅读:
    MSDN2010安装及使用(MSDN Library)[转]
    单元测试那些事
    Nhibernate Profiler安装中的问题
    ztree的动态添加
    事件与委托
    linux ls dir
    学习技术资料的思路
    linux目录结构
    是否需要深入了解java平台
    工作中心的改变
  • 原文地址:https://www.cnblogs.com/lexus/p/2356444.html
Copyright © 2011-2022 走看看