6

the following code will help illustrate my problem:

import java.util.Locale;
import java.text.*;

public class LocaleTest {
   public static void main(String[] args) {
      System.out.println(Locale.getDefault());
      System.out.println("java-version-" +System.getProperty("java.version"));
      System.setProperty("sun.locale.formatasdefault","true");
      System.out.println("prop:" +System.getProperty("sun.locale.formatasdefault"));
      System.out.println("getLocale-" +Locale.getDefault());
   }
}

As we know, there is bug in Java 7, in Locale.getDefault().However as recommended by Oracle I have set the system property 'sun.locale.formatasdefault' to true. Even though I am now getting my m/c Locale, it is always showing as en_US even though my m/c Locale is set to fr_BE.

Here is the output of above code, which is compiled and run on Java 1.7.0_09:

en_US
java-version-1.7.0_09
prop:true
getLocale-en_US

Any thoughts on what might be causing thus? Many thanks in advance.

Perception
  • 79,279
  • 19
  • 185
  • 195
vish
  • 61
  • 4

1 Answers1

4

You need to set that system property before starting up your JVM. You can do this via command line arguments:

java -Dsun.locale.formatasdefault=true TargetClass

Or in environments where you don't control the launching of the JVM, you can set it via _JAVA_OPTIONS environment variable:

  • *Nix

    export _JAVA_OPTIONS=-Dsun.locale.formatasdefault=true
    
  • Windows

    SET _JAVA_OPTIONS=-Dsun.locale.formatasdefault=true
    

In Windows, if you want the change to be applied not only for that CMD but systemwide, you create a Windows System Variable JAVA_TOOL_OPTIONS

Alfredo Osorio
  • 11,297
  • 12
  • 56
  • 84
Perception
  • 79,279
  • 19
  • 185
  • 195
  • thanks for your reply, can you please elaborate how to set system property before starting up JVM.If I run the program as java -Dsun.locale.formatasdefault=true className then it works.I need to this works on application where there are number of classes,then how I do that. – vish Nov 29 '12 at 12:25
  • That's exactly how you would specify the property before startup. You can also set the property up in your environment before launching your program or IDE, or even on a per program level ***within*** the IDE – Perception Nov 29 '12 at 12:43
  • thanks Perception, I am very much close to the answer but having little confusion on this, so can you please elaborate that how to set SET sun.locale.formataadefault=true Windows in environment property i.e which file needs to be changed. – vish Nov 29 '12 at 13:05
  • Heres how you set your environment variables on Windows, hope this helps - http://vietpad.sourceforge.net/javaonwindows.html – Perception Nov 29 '12 at 15:18
  • Setting environment variables has no effect. However, you can use `_JAVA_OPTIONS` environment variable: `set _JAVA_OPTIONS=-Dsun.locale.formatasdefault=true`. See [*Information about `_JAVA_OPTIONS`*](http://stackoverflow.com/q/17781405/572834) for more. – Alexey Ivanov Jul 27 '15 at 13:54