最近有個功能需要java與python之間的數據交互,java需要把參數傳給python,然后python計算的結果返回給java.于是就寫了一個工具類.
首先,maven 需要加載jython的依賴.工具類代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
import java.util.list; import java.util.map; import java.util.properties; import org.apache.poi.ss.formula.functions.t; import org.python.core.pyfunction; import org.python.core.pyinteger; import org.python.core.pyobject; import org.python.core.pystring; import org.python.util.pythoninterpreter; /** * @classname: jythonutils * @description:todo(jython 工具類) * @author: zy * @date: * * @copyright: 2018 inc. all rights reserved. * 注意: */ public class jythonutils { /** * @title: jythoninit * @description: todo(初始化jython) * @param: @return * @return: pythoninterpreter * @throws */ public static pythoninterpreter jythoninit(){ //初始化site 配置 properties props = new properties(); props.put( "python.home" , "" ); //python lib 或 jython lib,根據系統中該文件目錄路徑 props.put( "python.console.encoding" , "utf-8" ); props.put( "python.security.respectjavaaccessibility" , "false" ); props.put( "python.import.site" , "false" ); properties preprops = system.getproperties(); pythoninterpreter.initialize(preprops, props, new string[ 0 ]); //創建pythoninterpreter 對象 pythoninterpreter interp = new pythoninterpreter(); return interp; } /** * @title: loadpythonfile * @description: todo(加載python 源碼文件,) * @param: @param interp * @param: @param filepath ,比如:f:\\jpython_jar\\jpythontest\\pythontest.py 或/testpython/test.py * @param: @return * @return: pythoninterpreter * @throws */ public static pythoninterpreter loadpythonfile(pythoninterpreter interp, string filepath){ interp.execfile(filepath); return interp; } /** * @title: loadpythonfunc * @description: todo(加載python 源碼文件中的某個方法) * @param: @param interp * @param: @param functionname * @param: @return * @return: pyfunction * @throws */ public static pyfunction loadpythonfunc(pythoninterpreter interp, string functionname){ //加載方法 pyfunction func = (pyfunction) interp.get(functionname,pyfunction. class ); return func; } /** * @title: execfunc * @description: todo(執行無參方法,返回pyobject) * @param: @param func * @return: pyobject * @throws */ public static pyobject execfunc(pyfunction func){ pyobject pyobj = func.__call__(); return pyobj; } /** * @title: execfunctostring * @description: todo(執行無參方法,返回一個字符串) * @param: @param func * @param: @return * @return: string * @throws */ public static string execfunctostring(pyfunction func){ pyobject pyobj = execfunc(func); return (string) pyobj.__tojava__(string. class ); } /** * @title: execfunctostring * @description: todo(執行有參方法,返回一個字符串) * @param: @param func * @param: @param paramname ,參數名 * @param: @return * @return: string * @throws */ public static string execfunctostring2(pyfunction func, string paramname){ pyobject pyobj = func.__call__( new pystring(paramname)); return (string) pyobj.__tojava__(string. class ); } /** * @title: execfunctointeger * @description: todo(執行無參方法,返回一個integer) * @param: @param func * @param: @return * @return: integer * @throws */ public integer execfunctointeger(pyfunction func){ pyobject pyobj = execfunc(func); return (integer) pyobj.__tojava__(integer. class ); } /** * @title: execfunctolist * @description: todo(執行無參方法,返回一個list) * @param: @param func * @param: @return * @return: list<t> * @throws */ public list<t> execfunctolist(pyfunction func){ pyobject pyobj = execfunc(func); return (list<t>) pyobj.__tojava__(list. class ); } /** * @title: execfunctomap * @description: todo(執行無參方法,返回一個map<string, object>) * @param: @param func * @param: @return * @return: map<string,object> * @throws */ public map<string, object> execfunctomap(pyfunction func){ pyobject pyobj = execfunc(func); return (map<string, object>) pyobj.__tojava__(map. class ); } public void execfunctobyparamslist(pyfunction func, list<t> paramslist){ } public static void main(string[] args){ pythoninterpreter interp = jythoninit(); //文件名 string filepath = "f:\\jpython_jar\\jpythontest\\pythontest.py" ; interp = loadpythonfile(interp, filepath); //函數名 string functionname = "count" ; pyfunction func = loadpythonfunc(interp, functionname); //執行無參方法,返回pyobject pyobject pyobj = execfunc(func); //執行無參方法,返回string string resultstr = execfunctostring(func); //執行有參方法,返回string string paramname = "name" ; string resultstr2 = execfunctostring2(func, paramname); } } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/cafebar123/article/details/79394431