Jython 與 Java

來源:互聯網
上載者:User

這兩天因為工作需要,需要在Java Jvm 進程內調用Python指令碼。下了Jython練練手,指令碼語言看著真彆扭啊。若干年前寫自動化測試載入器時也用過python一小陣子,但基本忘光光了。好了,直奔主題。

 

前提:

1. sun-jre1.6, jython 2.5

2. 在官網下下個jython_installer-2.5.0.jar,一路next, 在 /jython-install-path/裡有個jython.jar, 把這個jython.jar import 進Java Project 裡邊。

 

代碼:

 

fibo.py

--------------------------------------------------------------------------------------------

 # Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n

    a, b = 0, 1

    while b < n:

        print b,

        a, b = b, a+b

def fib2(n): # return Fibonacci series up to n

    result = []

    a, b = 0, 1

    while b < n:

        result.append(b)

        a, b = b, a+b

    return result

------------------------------------------------------------------------------------------------------

 

TestJython.java

 

----------------------------------------------------------------------------------------------------------

package platform.oaas.******.other.test;

import org.python.core.PyException;
import org.python.core.PyFile;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;

public class TestJython {
   
    /**
     * 關注Jython這幾個方面的內容:
     * 1. 怎樣從某個指定的路徑import一個Jython模組?
     * 2. 怎樣調用模組裡的方法?
     * 3. java 與 jython 間的參數應該怎樣進行傳遞?
     *
     * @param args
     * @throws PyException
     */
    public static void main(String []args)throws PyException
    {
        PythonInterpreter interp = new PythonInterpreter();
       
        // 1.
        // 引入系統模組,並將[./pythonsrc]加入模組搜尋路徑中去。
        interp.exec("import sys");
        interp.exec("sys.path.append('./pythonsrc')");
       
        // 2. 引入 fibo, 執行fibo.fib(100), ok
        interp.exec("import fibo");
        interp.exec("fibo.fib(100)");
   
        // 3. 在Java 程式中獲得 python內建類型的變數值。
        String text = "'this is a bad day'";
        interp.exec("x = " + text + " + '!!!'");
        interp.exec("print x");
        PyObject object = interp.get("x");
        PyString xString = object.__str__();
        String outX = xString.asString();
       
        interp.exec("_file = open('/home/fore/work/OaasSearch/search/oaas_search/workspace/oaas-search0.1/pythonsrc/fibo.py')");
        object = interp.get("_file");
        PyFile file = (PyFile)object;
        PyString content = file.read(100);
        System.out.println(content);
       
        interp.exec("array = _file.read(100)");
        String array = interp.get("array").asString();
        System.out.println(array);
       
        interp.exec("_file.close()");
        file.close();
    }
}
------------------------------------------------------------------------------------------------------------------------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.