从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");