How to run python command using java

Whenever developer use java language as backend but  some script has been written  in python. so ,  we need to run command for that . I have written some code to run command  

Java code to run python program .

import java.io.*;

class openCmd{

  public static void main(String[] args) {

  

  try {

    Process process = Runtime.getRuntime().exec((new String[]{"python","main.py","arg1" ,"arg2","arg.."}));

    System.out.println(process.getErrorStream());

    BufferedReader reader = new BufferedReader(

            new InputStreamReader(process.getInputStream()));

    String line;

    while ((line = reader.readLine()) != null) {

     //print output 

       System.out.println(line);

    }

    reader.close();


   }catch (IOException e) {

    e.printStackTrace();

  }

  }

}

Explanation  :

This code open command prompt and insert command. Also , return output .


Comments