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();
 }
asked Mar 29, 2018 at 7:26
3
  • Wait to get exit value - maybe still alive? check ps Commented Mar 29, 2018 at 7:29
  • I think you should use python to execute your script and not CMD. have a look here Commented Mar 29, 2018 at 8:57
  • Thanks ZeusNet i followed what you said and worked for me. Commented Mar 29, 2018 at 16:33

1 Answer 1

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.

answered Mar 29, 2018 at 9:04
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks EJP i have closed input stream and also followed ZeusNet suggestion to include python Python instead of cmd and its worked for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.