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
user3853393
2531 gold badge7 silver badges26 bronze badges
1 Answer 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
user207421
312k45 gold badges324 silver badges493 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
user3853393
Thanks EJP i have closed input stream and also followed ZeusNet suggestion to include python Python instead of cmd and its worked for me.
lang-java
ps