Java教程

在idea的springboot项目中运行PyCharm的python文件

本文主要是介绍在idea的springboot项目中运行PyCharm的python文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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("结束");

    }
这篇关于在idea的springboot项目中运行PyCharm的python文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!