Java调用Python脚本文件
1.查看eclipse版本(启动eclipse--Help--About Eclipse IDE)
2.查看jdk版本1.8
3.Python3.6
4.下载pydev https://www.pydev.org/update_sites/7.2.0/
(下载之后解压有两个文件夹features、plugins分别将里面的内容放入D:\eclipse里面的同名文件夹)
5.完成第四步之后,按照如下图操作:Window--Preference--PyDev--Interpreters--Pyhton Interpreters--Config first in PATH--Libraries---Apply and close
6.创建PyDev项目:File--New--PyDev Project
7.测试代码
Java调用Python脚本文件:在eclipse中运行Python文件,输出Python文件的结果;如果输出0则表示成功、输出1则表示失败。
Java代码:
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class test {
public static void main(String[] args) {
String[] arguments = new String[] {"python", "D:\\python数据分析\\test.py"};
try {
Process process = Runtime.getRuntime().exec(arguments);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
//java代码中的process.waitFor()返回值为0表示我们调用python脚本成功,
//返回值为1表示调用python脚本失败,这和我们通常意义上见到的0与1定义正好相反
int re = process.waitFor();
System.out.println(re);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Python代码:
def hello():
print('Hello,Python')
if __name__ == '__main__':
hello()