PakRulez
PakRulez

Reputation: 31

How to give jar file output back to ProcessBuilder?

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);
}
}

Upvotes: 1

Views: 1278

Answers (2)

Binil Thomas
Binil Thomas

Reputation: 13779

Here is some sample code I tried and got working.

The program launches an external process using ProcessBuilder. It also spawns two new threads to:

  1. read output & error streams of the new process and write it to the program's System.out
  2. copy everything written on the program's System.in to the input stream of the new process

This works well, but I ran into some trouble getting to stop the thread which reads from System.in to shutdown gracefully. I could not find a way to interrupt a thread blocked on reading from System.in, so I worked around it by setting the thread as a daemon.

Here are couple of good resources on these topics:

  1. Interrupting Java threads
  2. When Runtime.exec() won't

Upvotes: 0

PakRulez
PakRulez

Reputation: 31

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.

public ArrayList<Boolean> pro(String b) throws IOException, Exception {
        ArrayList<Boolean> ar = new ArrayList<Boolean>();
        try {
            ProcessBuilder pb = new ProcessBuilder("java", "-jar",
                    "myjar.jar");

            pb.directory(new File("path to myjar"));
            Process proc = pb.start();

            Scanner in = new Scanner(proc.getInputStream());
            PrintWriter out = new PrintWriter(proc.getOutputStream());

            // Read user input
            out.println(b);
            out.flush();
            String response = new String();

            // Read the response
            for (int i = 0; i < b.length(); i++)
                response = in.nextLine();

            in.close();
            out.close();

            StringTokenizer st = new StringTokenizer(response);
            while (st.hasMoreTokens()) {
                String bol = st.nextToken();

                if (bol.equals("true"))
                    ar.add(true);
                else
                    ar.add(false);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return ar;
    }

Upvotes: 2

Related Questions