Q:

Executing Commands to a Process

So I have this code:
String[] command = new String[] { "java", "-jar", file.getName() };
try {
    System.out.println("Loading input/output");
    final Process process = Runtime.getRuntime().exec(command);
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    final OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream());
    final BufferedReader consolein = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Loaded input/output");
    new Thread() {
        @Override
        public void run() {
            while (true) {
                System.out.println("hi");
                try {
                    String temp = consolein.readLine();
                    out.write(temp);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
    while (true) {
        System.out.println(in.readLine());
    }
} catch (IOException e) {
    e.printStackTrace();
}

And I am trying to execute commands the the running jar. Input is fine, I see all of the messages in console. I added the "hi" message to test if it was working. It shows up whenever I input something. However, whatever I type is NOT being sent to the running program. What am I doing wrong here?

A:

Depending on what the process you are calling is expecting you may not need a newline; you may just need to call flush() on the OutputStreamWriter out.
String temp = consolein.readLine();
out.write(temp);    
out.flush();

See http://docs.oracle.com/javase/7/docs/api/java/io/OutputStreamWriter.html

