1 什么是jython?
他其實是一門語言,并非是Java 或者Python的解釋器.用它可以實現,java和python代碼的互相訪問。
2 簡單的例子
java中執行python 語句
1
2
3
|
PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec( "days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); " ); interpreter.exec( "print days;" ); |
java調用python的腳本:
1
2
|
PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile( "script.py" ); |
java調用python類當中的函數
先在python文件中定一個python函數
1
2
3
|
def pluser(a,b): # print "the result of pluser is %d" % (a+b) return a + b |
在java當中去調用:
1
2
3
4
|
PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile( "F:\\machine learning\\machinelearninginaction\\Ch02\\test.py" ); PyFunction function = (PyFunction)interpreter.get( "pluser" ,PyFunction. class ); PyObject o = function.__call__( new PyInteger( 8 ), new PyInteger( 23 )); |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/rually/article/details/51320477