1、通过Runtime进行调用
1.1 传一个字符串
- java文件
public class CoalType {
public static String coalType_list(String data) {
String arr = null;
try {
String[] arg = new String[] { "E:\\Program Files\\Python36\\python.exe", "E:\\Test\\test\\qiting.py",data};
Process proc = Runtime.getRuntime().exec(arg);// 执行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) {
String data = "Lisa";
String test_name = CoalType.coalType_list(data);
System.out.println(test_name);
}
}
- py文件
import numpy as np
import sys as ss
def main():
print (ss.argv[1])
if __name__ == '__main__':
main()
- 结果
Lisa
如下图: