springboot运行程序(java运行python文件)


(一)目的:实现web后台调取python脚本

(二)实现:

  1、方案1使用Runtime.getRuntime().exec(command)函数

  (1)在控制映射处写Runtime.getRuntime().exec(command)函数

  @RequestMapping("/openLight")
    public ModelAndView openLight() throws IOException {
        ModelAndView mav = new ModelAndView("openLight");
        String command = "cmd.exe /c cd"
                + " E:\\projects\\python\\smarthome\\venv\\Include "
                + "&& start python main.py";
        Process p = Runtime.getRuntime().exec(command);
        return mav;
    }

  (2)该文件是运行E:\\projects\\python\\smarthome\\venv\\Include 下的main.py以此实现简单的调用

if __name__ == '__main__':
    fp = open("test.txt", 'w')

  注意:

    在window下用\表示路径,而在linux都是用/表示路径。在有路径需要修改的时候,要注意区分。

更多方法请参考:https://blog.csdn.net/qq_26591517/article/details/80441540


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM