I am executing a simple jar file by using process builder. The jar file simply computes the square of a number by asking the user to enter a number. I can enter a number at console but the program hangs. How can I pass this (number) back to process builder for its square to be computed by the jar file?
Here is my code for this:
public static void executeSystemCommand(String... args) {
try {
ProcessBuilder pb = new ProcessBuilder(args);
pb.redirectErrorStream(true);
Process proc = pb.start();
Reader reader = new InputStreamReader(proc.getInputStream());
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
reader.close();
Scanner scan = new Scanner(System.in);
int k = scan.nextInt();
proc.getOutputStream().write(k);
proc.getOutputStream().close();
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
Here is a code that inputs a string b to the jar file and returns an arraylist of booleans on the basis of output received from the jar file.