
1 | public class CoalType { |
- py文件
1 | import numpy as np |
- 结果
1 | Lisa |
如下图:

在Java中通过Runtime调用Python程序与直接执行Python程序的效果是一样的,可以在Python中读取传递的参数,也可以在Java中读取到Python的执行结果。
如上图所示,这样是获取不到数据的,需要输出语句print,StartStopOpt方法在java中也调用不了,需要在.py文件里初始化调用,然后输出,java才能获取到数据
需要注意的是,不能在Python中通过return语句返回结果,只能将返回值写入到标准输出流中,然后在Java中通过标准输入流读取Python的输出值。
1.1 传一个list集合
java文件
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
34public class Mogun {
public static String coalType_list(List<Double> list_one) {
String arr = null;
try {
String sysPython = "E:\\Program Files\\Python36\\python.exe ";
String filePython = "E:\\Test\\test\\qiting.py ";
Process proc = Runtime.getRuntime().exec(sysPython + filePython + list_one);// 执行py文件
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
arr = line;
}
in.close();
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return arr;
}
public static void main(String[] args) throws Exception {
List<Double> list_one = new ArrayList<>();
list_one.add(1.222);
list_one.add(2.888);
list_one.add(3.888);
list_one.add(4.888);
list_one.add(5.888);
String flag = Mogun.coalType_list(list_one);
System.out.println(flag);
}
}py文件
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
26import numpy as np
import sys as ss
def StartStopOpt(list_str,list_float):
............
...........
return result
def main():
list_str = []
list_float = []
for i in range(1, len(ss.argv)):
list_str.append(ss.argv[i].replace(",", ""))
list_str[0] = list_str[0].replace("[", "")
list_str[len(ss.argv) - 2] = list_str[len(ss.argv) - 2].replace("]", "")
# 字符串数组
# print(list_str)
# 浮点数数组
list_int = list(map(float, list_str))
# print(list_int)
# 调用函数传参得到返回值输出
test = StartStopOpt(list_str,list_float)
print(test)
if __name__ == '__main__':
main()结果
1
2
3
4
5
6字符串数组
['1.222', '2.888', '3.888', '4.888', '5.888']
浮点数数组
[1.222, 2.888, 3.888, 4.888, 5.888]
调用函数传参得到返回值输出
...省略
注意:一定要记得空格,不然会报错


1.1 传两个list集合
java文件
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
40public class Mogun {
public static String coalType_list(List<Double> list_one, List<Integer> list_two) {
String arr = null;
try {
String sysPython = "E:\\Program Files\\Python36\\python.exe ";
String filePython = "E:\\Test\\test\\qiting.py ";
Process proc = Runtime.getRuntime().exec(sysPython + filePython + list_one + list_two);// 执行py文件
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
arr = line;
}
in.close();
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return arr;
}
public static void main(String[] args) throws Exception {
List<Double> list_one = new ArrayList<>();
list_one.add(1.222);
list_one.add(2.888);
list_one.add(3.888);
list_one.add(4.888);
list_one.add(5.888);
List<Integer> list_two = new ArrayList<>();
list_two.add(1);
list_two.add(2);
list_two.add(3);
list_two.add(4);
list_two.add(5);
String flag = Mogun.coalType_list(list_one, list_two);
System.out.println(flag);
}
}py文件
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
36import numpy as np
import sys as ss
def StartStopOpt(turn,current):
............
...........
return result
def main():
#初始数据
# print (ss.argv[1:])
list_str = []
turn = []
current = []
for i in range(1, len(ss.argv)):
list_str.append(ss.argv[i].replace(",", ""))
list_str[0] = list_str[0].replace("[", "")
list_str[len(ss.argv) - 2] = list_str[len(ss.argv) - 2].replace("]", "")
str = ','.join(list_str)
arr = str.split("][")
turn = arr[0].split(",")
turn = list(map(float, turn))#转换成浮点数数组
#输出第一个数组
# print (turn)
current = arr[1].split(",")
current = list(map(int, current))#转换成整数数组
#输出第二个数组
# print (current)
# 调用函数传参得到返回值输出
test = StartStopOpt(turn,current)
print(test)
if __name__ == '__main__':
main()结果
1
2
3
4
5
6
7
8初始数据
['[1.222,', '2.888,', '3.888,', '4.888,', '5.888][1,', '2,', '3,', '4,', '5]']
输出第一个数组
[1, 2, 3, 4, 5]
输出第二个数组
[1.222, 2.888, 3.888, 4.888, 5.888]
调用函数传参得到返回值输出
...省略
问题:数组的长度也是有限制的,我试着添加了8000元素的double类型的集合,就报错了
解决:https://www.cnblogs.com/downmoon/archive/2012/11/18/2775587.html