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, in Java 7 there is an error in Locale.getDefault (). However, as recommended by Oracle, I set the system property "sun.locale.formatasdefault" to true. Despite the fact that now I get my m / c Locale, it always displays as en_US, although my m / c Locale is set to fr_BE.

Here is the result of the above code that 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 could be causing? Thank you very much in advance.

+6
source share
1 answer

You need to set this system property before by running your JVM. You can do this with command line arguments:

 java -Dsun.locale.formatasdefault=true TargetClass 

Or in environments where you do not control the start of the JVM, you can set it through the _JAVA_OPTIONS environment _JAVA_OPTIONS :

  • * Nix

     export _JAVA_OPTIONS=-Dsun.locale.formatasdefault=true 
  • for windows

     SET _JAVA_OPTIONS=-Dsun.locale.formatasdefault=true 

On Windows, if you want the change to be applied not only for this CMD, but for the entire system, you create the Windows system variable JAVA_TOOL_OPTIONS

+4
source

All Articles