简体   繁体   中英

Calling python file from java throws error

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM