Nov 8, 2018

Sample code to call python script from java


public static void main(String[] args) throws IOException {

        try {
            Runtime r = Runtime.getRuntime();
            Process p = r.exec("python3.2 urlopen.py");
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            p.waitFor();
          
            while (br.ready()) {
                System.out.println(br.readLine());
            }

        } catch (Exception e) {
            String cause = e.getMessage();
            if (cause.equals("python: not found")) {
                System.out.println("No python interpreter found.");
            }
        }
    }