Idea中通过java程序直接调用python文件 使用Runtime.getRuntime()执行脚本文件 1 在idea中导入下载好的jar包
解决springboot运行python脚本出现乱码问题 Idea设置,修改编码为GBK Pycharm设置,修改编码为GBK
导入python包 定义python脚本路径 运行python脚本,即可
部分代码如下
import org.python.util.PythonInterpreter;
private static String PATH="D:\\PyCharm\\project\\test.py";
@RequestMapping("/openPython")
public void openPython() throws IOException, InterruptedException {
System.out.println(System.getProperty("file.encoding"));
// System.out.println(Charset.defaultCharset().name());
System.out.println("开始");
final ProcessBuilder processBuilder = new ProcessBuilder("python", PATH);
processBuilder.redirectErrorStream(true);
final Process process = processBuilder.start();
final BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s = null;
while ((s = in.readLine()) != null) {
System.out.println(s);
}
final int result = process.waitFor();
System.out.println("执行结果:" + result);
System.out.println("结束");
}
|