Java调用python文件
本方法的运行环境为:jdk15.0
使用的编辑软件为:idea Ultimate和jupyter notebook
主要内容为:Java通过BufferedReader获取无参数的.py文件和Java通过BufferedReader获取含参数的.py文件
Java代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* @author :
* @date 2022/6/21 21:37
* please don't copy
**/
public class test {
public static void main(String[] args) throws Exception {
Process process,process1;
//此处为不带参数的python代码调用
String [] test1=new String[]{
"D:\\softwareOfAI\\pythonPC\\pc\\python.exe" ,
"D:\\softwareOfLife\\Browser\\Download\\s.py"
};
String line;
process = Runtime.getRuntime().exec(test1);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
while ((line=buffer