从java代码运行Python脚本 [英] Run Python script from java code

查看:163
本文介绍了从java代码运行Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在java中尝试python。
我试图从我的代码执行python脚本,如下所示。

This is the first time I am trying python in java. I am trying to execute python script from my code as follows.

    Process process = Runtime.getRuntime().exec("python C:\\Users\\username\\Desktop\\demo\\filename.py");

但我得到以下异常

无法运行程序python:CreateProcess error = 2,系统找不到指定的文件

"Cannot run program "python": CreateProcess error=2, The system cannot find the file specified"

我已经安装了python。
我不确定为什么找不到该文件。
我试图按照链接但它没有解决我的问题。

I have installed python. I am not sure why the file is not found. I tried to follow this link but it did not solve my issue.

提前致谢。

编辑1

我尝试了Viacheslav Vedenin给出的示例代码,它在我执行我的java(servlet)程序时起作用。但是当我从JSP按钮单击事件运行相同的功能时,它不起作用。
它给了我以下错误

I tried the sample code given by "Viacheslav Vedenin", it worked when I executed my java(servlet) program. But when I ran the same function from JSP button click event, it did not work. It gave me following error

java.io.IOException: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified 

请帮我解决此问题。

推荐答案

尝试使用python的完整路径,例如

Try to use full path to python, for example

Process process = Runtime.getRuntime().exec("C:\\Python\\python.exe  
          C:\\Users\\username\\Desktop\\demo\\filename.py");

这篇关于从java代码运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆