5

Echoing the conclusions of https://stackoverflow.com/a/17177904/14731, applications need to invoke WriteConsoleW or chcp in order to output unicode characters to the Windows console.

I don't want to use JNI so the WriteConsoleW approach is out. Is it possible for a Java application to invoke chcp on the console it is running inside of?

As far as I know, invoking Runtime.exec("cmd.exe", "/c", "chcp", "65001") will create a new console, change its code-page, and then kill the console. Meaning, the existing console won't be affected.

Community
  • 1
  • 1
Gili
  • 86,244
  • 97
  • 390
  • 689

1 Answers1

10

Based on a hunch, I tried:

ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "chcp", "65001").inheritIO();
Process p = pb.start();
p.waitFor();

and it worked!

inheritIO() causes the child process to inherit the parent's stdout. When chcp modifies the character encoding of the child stdout it actually ends up modifying the parent's encoding as well. Great success! :)

Gili
  • 86,244
  • 97
  • 390
  • 689