So, you clear the two inputs separately from your batch script and close the stream. As a result, the script consumes only the first input. And since the stream is closed then, it simply omits all the remaining SET /P expressions, ignoring the actual inputs remaining in the pipe. And the question is: why?
I found this article that analyzes how the SET /P command works in detail and what conditions exist to end a single input prompt session.
The last 2 char reads are CRLF
This is a normal case and preferred. If the input is not transmitted but entered by the user, the input ends when the ENTER key has been hacked. Then the CRLF tokens were set, and the input is consumed. In our Java channel, we can do the same by adding \r\n to the end of the String . But for some reason this does not work, and I could not understand why. Time-out
A very simple workaround that I could offer you is to just wait a while between the entrances. The result will be that the script consumes each input until the next. Therefore, if you add
Thread.sleep(1000);
between you two inputs everything will work as expected.
full buffer
The input of one SET /P statement is limited to 1024 characters. We can use this to solve this problem by combining a buffer that is equal to char less than 1024. Thus, the first char of the next input will make the buffer full, which will consume the input and move to the next expression. Therefore, if you use this conversion method
public static byte[] convert(String str){ byte[] asBytes = str.getBytes(); byte[] result = new byte[1023]; for(int i = 0; i < asBytes.length; i++) result[i] = asBytes[i]; return result; }
Your code will work properly. In fact, you can even refuse to rinse.
OutputStream out = process.getOutputStream(); out.write(convert("tester")); out.write(convert("25")); out.close();
source share