3

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

But I am getting following exception

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

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.

Thanks in advance.

Edit 1

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 

Please help me to resolve this issue.

1
  • You can add to the path Enviroment Variable the path of python soo thenyou will be able to execute a python command Commented Jun 30, 2017 at 11:20

1 Answer 1

3

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");
Sign up to request clarification or add additional context in comments.

2 Comments

I tried above sample and it worked when I ran my java 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.