^D
isn't a character; it's a command interpreted by your shell telling it to close the stream to the process (thus the process receives EOF on stdin
).
You need to do the same in your code; flush and close the OutputStream
:
String usage =
".Dd \\[year]\n" +
".Dt test 1\n" +
".Os\n" +
".Sh test\n";
...
OutputStream out = groff.getOutputStream();
out.write(usage.getBytes());
out.close();
...