1

I am trying to call python file from java. but it throws following error.

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

The code that I have tried is :

    Process p = Runtime.getRuntime().exec("python C:\\Project\\Script\\Test.py");
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    System.out.println(in.readLine());

what could be the problem?

1

1 Answer 1

0

Runtime.exec expects a file with no path information to be in the user dir and not in the directory you specify to use as working directory. Try using this code once.

Runtime rt = Runtime.getRuntime();
Process prs;
File Dir_temp = new File("C:\\Project\\Script\\");
prs = rt.exec(new File(Dir_temp, "Test.py").getAbsolutePath(), null, Dir_temp);
prs.waitFor();
prs.destroy();
Sign up to request clarification or add additional context in comments.

1 Comment

again it throws java.io.IOException: Cannot run program "C:\Project\FBLogin\Test.py" (in directory "C:\Project\Script"): CreateProcess error=193, %1 is not a valid Win32 application

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.