Writing in child process
The simplest way to resolve the problem is to retrieve the method return Runtime#exec()
", which is of type Process
and then using the Process#getOutputStream
to be able to write directly to the input of the process.
Example:
Process p = Runtime.getRuntime().exec("java MinhaClasse);
OutputStream stdin = process.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
writer.write("Valor 1\n");
writer.write("Valor 2\n");
writer.write("Valor 2\n");
writer.flush(); // pode chamar várias vezes, dependendo do volume
writer.close(); // opcional, somente quando acabar a entrada
The line break ( \n
) after the values serve to emulate the Enter key that the user presses when entering a value for Scanner
.
Beware of buffers and deadlocks
Something very important is that whenever you write something to the child process you must call the flush()
method of OutputStream
.
This is because OutputStream
may have some buffer that may not immediately get to the child process, leaving it locked indefinitely.
And such care is all the more important if the parent process reads the child's output, because in that case the two can be locked indefinitely.
ProcessBuilder
Another tip is that using ProcessBuilder
gives you greater flexibility in building the child process.
This can make a difference if you need to pass more parameters or something more complex.
Alternative # 1 - using native resources
An alternative would still be to use the command line power of the operating system and make the pipe for the entry of the Java program from some text file. Example:
cat input.txt | java MinhaClasse
Alternative # 2 - using a file as input
Another alternative to competition is to not pass the data through programming in the main process. I believe this could influence the runtime of the program.
The idea would be to put the input file in a read-only directory on the disk and pass the path to this file as a parameter to the program.
Then, simply instruct participants to create the Scanner
" based on a constructor that receives a file.
Example:
public static void main(String[] args) {
Scanner input = new Scanner(new File(args[0));
...
}
Much simpler, with no implementation difficulty or execution risk.