Running a .py file from Java

前端 未结 5 1563
春和景丽
春和景丽 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:37

    You can use like this also:

    String command = "python /c start python path\to\script\script.py";
    Process p = Runtime.getRuntime().exec(command + param );
    

    or

    String prg = "import sys";
    BufferedWriter out = new BufferedWriter(new FileWriter("path/a.py"));
    out.write(prg);
    out.close();
    Process p = Runtime.getRuntime().exec("python path/a.py");
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String ret = in.readLine();
    System.out.println("value is : "+ret);
    

    Run Python script from Java

提交回复
热议问题