The line that creates the problem is
cmd.append("2>&1");
You forgot to insert a space in front of the output redirection:
cmd.append(" 2>&1");
Without the space the last command is
echo %username%2
and the output is redirected to
stdout
. But that does not work because the input is already from
stdout
so that the redirection fails with an error message on
stderr
which is not catched by your code now.
Try it at a command prompt to see the error message:
echo %username%2>&1
The handle could not be duplicated during redirection of handle 1.