因项目分工,搭网络架构的同学用的java,写算法的同学用的python,现在需要将两者融合,网络上查到了一个简单方法。代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class hellow {
public static void main(String[] args) {
String[] arguments = new String[] {"python", "E:\\Demo\\RBF_NN_Python-master\\RBF_TRAIN_MORE.py"};
try {
Process process = Runtime.getRuntime().exec(arguments);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
int re = process.waitFor();
System.out.println(re);
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:.py文件中,所有读取文件,写入文件均要使用绝对路径,否则会出现问题。
大概原理:编译器调用CMD,去运行python程序,并将输出内容读取出来在当前界面中进行显示。