最近有个功能需要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);
}
}
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/cafebar123/article/details/79394431
相关文章
猜你喜欢
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 14
-
PHP中使用jQuery+Ajax实现分页查询多功能操作(示例讲解)
2025-05-29 26 -
2025-05-25 69
-
2025-06-04 80
-
2025-05-25 25
热门评论