vish
vish

Reputation: 61

Setting sun.locale.formatasdefault to true in Java 7

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.

Upvotes: 6

Views: 2341

Answers (1)

Perception
Perception

Reputation: 80603

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

Upvotes: 4

Related Questions