Running a .py file from Java

前端 未结 5 1564
春和景丽
春和景丽 2020-11-28 09:15

I am trying to execute a .py file from java code. I move the .py file in the default dir of my java project and I call it using the following code:

    Strin         


        
5条回答
  •  春和景丽
    2020-11-28 09:26

    I believe we can use ProcessBuilder

    Runtime.getRuntime().exec("python "+cmd + py + ".py");
    .....
    //since exec has its own process we can use that
    ProcessBuilder builder = new ProcessBuilder("python", py + ".py");
    builder.directory(new File(cmd));
    builder.redirectError();
    ....
    Process newProcess = builder.start();
    

提交回复
热议问题