title: Default character encoding for java console output
body: <p>How does Java determine the encoding used for <code>System.out</code>?</p>
  
  <p>Given the following class:</p>
  
  <pre><code>import java.io.File;
  import java.io.PrintWriter;
  
  public class Foo
  {
      public static void main(String[] args) throws Exception
      {
          String s = "xxäñxx";
          System.out.println(s);
          PrintWriter out = new PrintWriter(new File("test.txt"), "UTF-8");
          out.println(s);
          out.close();
      }
  }
  </code></pre>
  
  <p>It is saved as UTF-8 and compiled with <code>javac -encoding UTF-8 Foo.java</code> on a Windows system.</p>
  
  <p>Afterwards on a git-bash console (using UTF-8 charset) I do:</p>
  
  <pre><code>$ java Foo
  xxõ±xx
  $ java -Dfile.encoding=UTF-8 Foo
  xx├ñ├▒xx
  $ cat test.txt
  xxäñxx
  $ java Foo | cat
  xxäñxx
  $ java -Dfile.encoding=UTF-8 Foo | cat
  xxäñxx
  </code></pre>
  
  <p>What is going on here? </p>
  
  <p>Obviously java checks if it is connected to a terminal and is changing its encoding in that case. Is there a way to force Java to simply output plain UTF-8?</p>
  
  <hr>
  
  <p>I tried the same with the cmd console, too. Redirecting STDOUT does not seem to make any difference there. Without the file.encoding parameter it outputs ansi encoding with the parameter it outputs utf8 encoding.</p>