Run Python script from java code

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

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

      

But I am getting the following exception

"The program" python "cannot be started: CreateProcess error = 2, the system cannot find the file specified."

I have installed python. I'm not sure why the file was not found. I tried to follow this link, but it didn't solve my problem.

Thanks in advance.

Edit 1

I tried the sample code given by "Vyacheslav Vedenin", it worked when I was executing my Java (servlet) program. But when I ran the same function from the JSP button click event it didn't work. This gave me the following error:

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

      

Please help me to solve this problem.

+3


source to share


1 answer


Try using the full path to python like



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

      

+2


source







All Articles