1

Below is my code.Trying to execute python script but waitfor() never completing . Below is my code. Any suggestions.

String[] command ={"CMD","C:\\Users\\vkode200\\IdeaProjects\\Pythonex1\\TestHello.py"}; 

         ProcessBuilder probuilder = new ProcessBuilder(command );
            //You can set up your work directory
            /*probuilder.directory(new File("c:\\xyzwsdemo"));*/

            Process process = probuilder.start();

            //Read out dir output
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;
            System.out.printf("Output of running %s is:\n",
                    Arrays.toString(command));
            /*while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
*/
            //Wait to get exit value
            try {
                 exitValue = process.waitFor();
                 /*exitValue= process.exitValue();*/
                System.out.println("\n\nExit Value is " + exitValue);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
user3853393
  • 233
  • 4
  • 25

1 Answers1

1

You need to close the process's input stream, and consume all its output on both stdout and stderr, before calling waitFor().

Otherwise it can be blocked trying to read input you aren't sending, or produce output you aren't reading.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks EJP i have closed input stream and also followed ZeusNet suggestion to include python Python instead of cmd and its worked for me. – user3853393 Mar 29 '18 at 16:33