1

I'm currently trying to use processes in java to run a jar file. I'm able to run and read the content printed by the process. What I'm trying to achieve, is to write a command to the process. The jar file I'm running asks for user input and I am trying to allow the user to enter that input. This is my current code which isn't working:

public class Main {

public static void main(String[] args) {

    String command = "java -jar game.jar";

    Process process = executeCommand(command);

    CompletableFuture.runAsync(() -> {

        Scanner scanner = new Scanner(System.in);

        while (true) {

            String input = scanner.nextLine();

            if (input == null) {
                continue;
            }

            executeCommand(process, input);

        }

    });

    readOutput(process);

}

public static Process executeCommand(String command) {

    try {
        Process process = Runtime.getRuntime().exec(command);

        return process;
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }

}

public static List<String> readOutput(Process process) {

    List<String> output = new ArrayList<>();

    try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;
        while((line = reader.readLine()) != null) {
            System.out.print(line + "\n");
            output.add(line);
        }

        process.waitFor();

        return output;

    } catch (IOException ex) {
        ex.printStackTrace();
        return output;
    } catch (InterruptedException ex) {
        ex.printStackTrace();
        return output;
    }

}

public static void executeCommand(Process process, String command) {

    try {

        OutputStream out = process.getOutputStream();

        out.write(command.getBytes());

    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

}

3
  • You need a pipe stackoverflow.com/questions/4112470/… Commented Dec 17, 2018 at 1:05
  • How do you know nothing is received by the process? Maybe adding a \n at the end of the command may help. Commented Dec 17, 2018 at 1:09
  • Added \n didn't work. Nothing visibly changed Commented Dec 17, 2018 at 1:13

1 Answer 1

0

I was able to fix the problem by adding out.flush()

public static void executeCommand(Process process, String command) {

    try {

        OutputStream out = process.getOutputStream();

        out.write(command.getBytes());
        out.flush();

    } catch (IOException ex) {
        ex.printStackTrace();
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

It is dangerous to write in a blocking fashion to the output stream, it’s platform dependent how much if any buffer there is. You typically do that in a separate thread. But it might be enough to start the reader thread before you write in case it blocks because the target command wants to write something before it reads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.