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.

  • 相关阅读:
    HNOI2008玩具装箱
    CEOI2004锯木厂选址
    APIO2010特别行动队
    【HNOI2011】数学作业
    【JLOI2015】城池攻占
    魔法猪学院
    Kruskal重构树(货车运输)
    旋转卡壳求凸包直径
    Graham凸包算法简介
    poj-3169Layout
  • 原文地址:https://www.cnblogs.com/lexus/p/2356444.html
Copyright © 2011-2022 走看看