Christof Aenderl
Christof Aenderl

Reputation: 4512

Mac OS X, Java 7: createNewFile filename encoding

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();

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.

Upvotes: 2

Views: 2272

Answers (1)

Christof Aenderl
Christof Aenderl

Reputation: 4512

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.

Upvotes: 2

Related Questions