6

I have the following code:

public static void main( String[] args ) {
    System.out.println(Locale.getDefault());
    File f = new File("/Users/johngoering/Documents");
    File[] fs = f.listFiles();
    for (File ff : fs) {
        System.out.println(ff.getName());
        System.out.println(ff.exists());
    }
}

In my Documents folder I have a file called "öß.pdf". Here is the output under Java 6:

 en_US
 (...)
 öß.pdf
 true
 (...)

But here is the output under Java 7:

 en_US
 (...)
 o����.pdf
 false
 (...)

Note especially that file.exists returns false for a file returned by listFiles!! What gives? Is there any way to fix this? This seems like quite the Java 7 bug...

Epaga
  • 38,231
  • 58
  • 157
  • 245
  • uh...look at the code? System.out – Epaga Sep 20 '12 at 09:23
  • In the terminal out Java console or the IDE console? – MadProgrammer Sep 20 '12 at 09:24
  • You might try [Print Unicode characters to the Mac terminal](http://hints.macworld.com/article.php?story=20050208053951714) – MadProgrammer Sep 20 '12 at 09:26
  • but look especially at file.exists... why would file.exists be false? – Epaga Sep 20 '12 at 09:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16911/discussion-between-epaga-and-madprogrammer) – Epaga Sep 20 '12 at 09:28
  • 1
    The same is also true for applications launched with the JAR launcher (i.e. double click): They cannot see any files with umlauts. As File.exists = false, any directory filter will eliminate these - they simply don't show up. So this NEEDS TO BE FIXED - I cannot run my app from Eclipse all the time ! –  Dec 03 '12 at 17:12
  • Has anyone reported this bug to Oracle? As you figured, it's not specific to Eclipse, but to any java app. It bit me when using NetBeans and JVisualVM. – Christian Schlichtherle Dec 13 '12 at 21:04

1 Answers1

9

With some help from Oracle, we discovered a workaround: the environment variable LC_CTYPE was not set to UTF-8 within Eclipse (and when starting from a JNLP or wherever else). This explains why the code worked on the terminal, since the OS X terminal by default "sets the locale environment variables" (an option which can be turned off and then you get the same issue as above even in the terminal).

Setting this environment variable in the launcher worked around the problem.

I still consider this a bug for Java 7 because Java 6 still worked even WITHOUT this variable.

Epaga
  • 38,231
  • 58
  • 157
  • 245