2

I'm trying to update a application, running mainly on Mac OSX, from Java6 to Java7. It occurs that files beeing creating with some special characters in the filename e.g. "föhn.txt" are beeing created as "f?hn.txt" with Java7.

If you run this sort example on a Mac

File file = new File("föhn.txt");
file.createNewFile();
  • with Java6: föhn.txt
  • with Java7: f?hn.txt

I know there're simular threads for this topic:

and I've tryed to set JVM argument: -Dfile.encoding=UTF8 but this has no effect to the filename. Compiler and source are set to utf-8. I've no idea why this is not working with Java7 on OS X.

Update: I've tried the example within eclipse and Netbeans and the final application is bundled with the jdk (appbundler).

Mac OS X uses a decomposed UTF-8 format: File.listFiles() mangles unicode names with JDK 6 (Unicode Normalization issues)

And a blog about this topic: http://shlrm.org/blog/2012/10/04/osx-java-utf-8-oh-my/

This helps reading filenames but I've not found a solution for creating new files with the correct encoded filename.

Community
  • 1
  • 1
Christof Aenderl
  • 4,233
  • 3
  • 37
  • 48
  • 1
    Just made a test with Java 7 (1.7.0_06-ea) on OS X 10.7.5 and it creates the file with the correct name, shown correctly both in Terminal and Finder. Source file saved as UTF-8, no special switches on neither jvm nor the compiler. It is tested from the command line. – Monolo Feb 14 '13 at 11:03
  • Thanks for testing. I'm using 1.7.0_13 on OS X 10.8.2 and 10.7.5. I will try with an older Java version. Maybe a bug :-/ – Christof Aenderl Feb 14 '13 at 11:16
  • @Monolo you're right. Also works for me if I compile and run from command line, strange. – Christof Aenderl Feb 14 '13 at 15:22
  • I don't think the Eclipse console can handle the extended characters. Especially since you say that it works fine in Terminal. –  Feb 14 '13 at 15:27
  • 1
    I think your problem is similar to mine (http://stackoverflow.com/questions/13513652/encoding-issue-on-filename-with-java-7-on-osx-with-jnlp-webstart), but if you can, the solution to this (http://stackoverflow.com/questions/12987252/file-list-retrieves-file-names-with-non-ascii-characters-incorrectly-on-mac-os) worked for me when not starting from webstart – Duralumin Mar 13 '13 at 13:03

1 Answers1

2

Thanks @Duralumin for the right hint.

From eclipse, netbeans or a bundled app with JRE (appbundle) the java binary of Java7 is used directly. Calling java from the command line will use the binary under /usr/bin which is a file from Apple. For that reason it works from command line.

In eclipse or netbeans you have to set the Environment in the run configurations, just add:

LC_TYPE = "UTF-8"

In the Appbundle I added

setenv("LC_CTYPE","UTF-8",1);

to the native (Objective-C) JavaAppLauncher I'm using.

Christof Aenderl
  • 4,233
  • 3
  • 37
  • 48
  • For the Eclipse entry I think it should be LC_CTYPE = "UTF-8", at least that is what worked for me. Now wondering why this can't be the default on MacOS X? – Andre M Mar 11 '15 at 20:39