title: Java compiler platform file encoding problem
body: <p>Recently I encountered a file character encoding issue that I cannot remember ever having faced.  It's quite common to have to be aware of character encoding of text files and write code that handles encoding correctly when run on different platforms.  But the problem I found was caused by <strong>compilation</strong> on a different platform from the execution platform.  That was entirely unexpected, because in my experience when javac creates a class file, the important parameters are the java source and target params, and the version of the JDK doing the compile.  I my case, classes compiled with JDK 1.6.0_22 on Mac OS X behaved differently than classes compiled with 1.6.0_23-b05 on Linux, when run on Mac OS X. The specified source and target were 1.4.</p>
  
  <p>A String that was encoded as ISO-8859_1 in memory was written to disk using a PrintStream println method.  Depending on which platform the Java code was COMPILED on, the string was written differently.  This lead to a bug.  The fix for the bug was to specify the file encoding explicitly when writing and reading the file.</p>
  
  <p>What surprised me was that the behavior differed depending on where the classes were compiled, not on which platform the class was run.  I'm quite familiar with Java code behaving differently when run on different platforms.  But it is a bit scary when the same code, compiled on different platforms, <strong><em>runs differently on the same platform</em></strong>.</p>
  
  <p>Has anyone encountered this specific problem?  It would seem to bode ill for any Java code that reads and writes strings to file without explicitly specifying the character encoding.  And how often is that done?</p>