We are trying to write to STDIN of a command using the Runtime exec in Java. We use cmd /c to execute the command as Runtime exec will not recognise it without the cmd /c. This should write "q" and newline to a file called output.txt. We can find the file, but it does not have any contents.
String cmd = "cmd /c TYPE CON>output.txt";
try{
process = Runtime.getRuntime().exec(cmd, null);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
bufferedWriter.write("q");
bufferedWriter.newLine();
bufferedWriter.flush();
bufferedWriter.close();
}catch(Exception e){}
We have also tried to use the output stream directly and write bytes without success.
It has to be a STDIN command, are we misunderstanding the process.getOutputStream()
We have verified that the same command works in the command prompt.
We are currently on Windows 10
We have tried provided solutions from stackoverflow and other sites, but to no avail.