前提:.py文件直接在cmd中是可以正常运行的,但我从 java中调用这个.py文件一直为null
后面发现是路径的原因,改了之后的代码如下
Process proc;
try {
//重点就在这个路径上,无效的情况不能直接写python这个命令,应该写绝对路径
proc = Runtime.getRuntime().exec("D:\\python\\python D:\\aa\\aa.py");// 执行py文件
Thread.sleep(2000);
//用输入输出流来截取结果
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
python的代码就是一个简单的输出
list =[]
a = 1
while a <11:
list.append(a)
a+=1
print(list)```