I have a code that starts a java process (i.e.: executing a compiled java code) via
ProcessBuilder builder = new ProcessBuilder("java", "Sample", "arg1", "arg2");
builder.redirectErrorStream(true);
Process process = builder.start();
Through this, I can basically process the output and errors
OutputStream stdin = process.getOutputStream(); // <- Eh?
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
// reader.readLine() blah blah
Now, how can I send input to the stdin
? That is, if the code executed by the process has a line that waits for an input as in:
Scanner scan = new Scanner(System.in);
String val = scan.nextLine();
System.out.println(val);
I tried this:
writer.write("I'm from the stdin!.");
writer.flush();
Though nothing happened. The console still waited for an input.
Any thoughts?
EDIT: The question was answered, as accepted below. I'm editing to show the faulty code (which I failed to include btw. Lol).
Before the writer.write()
part, I had a
String line;
line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
...from the stdin!\n");
. Nothing changed though. :| – Vladimirwriter.write("I'm from the stdin."); writer.write("\r\n"); writer.flush();
– Vladimir