Search code examples
javainternationalizationlocale

Change java.locale.providers via System.setProperty has no effect


To change the locale providers in java 9+ (I am using 21 right now) I can start java with:

-Djava.locale.providers=COMPAT,SPI

However I tried to set this property programmatically via:

System.setProperty("java.locale.providers", "COMPAT,SPI");

But this not take effect.

The project is a web based application and I add above code in ServletContextListener#contextInitialized method

@WebListener
public class StartUp implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent event) {
    System.setProperty("java.locale.providers", "COMPAT,SPI"); 
}

Solution

  • Refer to the documentation for class LocaleServiceProvider:

    The search order of locale sensitive services can be configured by using the java.locale.providers system property. This system property declares the user's preferred order for looking up the locale sensitive services separated by a comma. As this property value is read and cached only at the initialization of this class, users should specify the property on the java launcher command line. Setting it at runtime with System.setProperty(String, String) is discouraged and it may not affect the order.

    I believe that your servlet container should provide the capability to configure such [system] properties. Since I did not find any details about which servlet container you are using, I cannot help you further. I suggest you refer to that servlet container's documentation.