How do I set the default locale in the JVM?

JavaLocalizationGlobalization

Java Problem Overview


I want to set the default Locale for my JVM to fr_CA. What are the possible options to do this?

I know of only one option Locale.setDefault()

Java Solutions


Solution 1 - Java

You can set it on the command line via JVM parameters:

java -Duser.country=CA -Duser.language=fr ... com.x.Main

For further information look at Internationalization: Understanding Locale in the Java Platform - Using Locale

Solution 2 - Java

From the Oracle Reference: > The default locale of your application is determined in three ways. > First, unless you have explicitly changed the default, the > Locale.getDefault() method returns the locale that was initially determined > by the Java Virtual Machine (JVM) when it first loaded. That is, the > JVM determines the default locale from the host environment. The host > environment's locale is determined by the host operating system and > the user preferences established on that system. > > Second, on some Java runtime implementations, the application user can > override the host's default locale by providing this information on > the command line by setting the user.language, user.country, and > user.variant system properties. > > Third, your application can call the Locale.setDefault(Locale) > method. The setDefault(Locale aLocale) method lets your application > set a systemwide (actually VM-wide) resource. After you set the default locale with this > method, subsequent calls to Locale.getDefault() will return the newly > set locale.

Solution 3 - Java

You can use JVM args

java -Duser.country=ES -Duser.language=es -Duser.variant=Traditional_WIN

Solution 4 - Java

In the answers here, up to now, we find two ways of changing the JRE locale setting:

  • Programatically, using Locale.setDefault() (which, in my case, was the solution, since I didn't want to require any action of the user):

      Locale.setDefault(new Locale("pt", "BR"));
    
  • Via arguments to the JVM:

      java -jar anApp.jar -Duser.language=pt-BR
    

But, just as reference, I want to note that, on Windows, there is one more way of changing the locale used by the JRE, as documented here: changing the system-wide language.

> Note: You must be logged in with an account that has Administrative Privileges. > > 1. Click Start > Control Panel. > > 2. Windows 7 and Vista: Click Clock, Language and Region > Region and Language. > > Windows XP: Double click the Regional and Language Options > icon. > > The Regional and Language Options dialog box appears. > > 3. Windows 7: Click the Administrative tab. > > Windows XP and Vista: Click the Advanced tab. > > (If there is no Advanced tab, then you are not logged in with > administrative privileges.) > > 4. Under the Language for non-Unicode programs section, select the desired language from the drop down menu. > > 5. Click OK. > > The system displays a dialog box asking whether to use existing > files or to install from the operating system CD. Ensure that you have > the CD ready. > > 6. Follow the guided instructions to install the files. > > 7. Restart the computer after the installation is complete.

Certainly on Linux the JRE also uses the system settings to determine which locale to use, but the instructions to set the system-wide language change from distro to distro.

Solution 5 - Java

There is another away if you don't like to change System locale but the JVM. you can setup a System (or user) Environment variable JAVA_TOOL_OPTIONS and set its value to -Duser.language=en-US or any other language-REGION you want.

Solution 6 - Java

You can do this:

enter image description here

enter image description here

And to capture locale. You can do this:

private static final String LOCALE = LocaleContextHolder.getLocale().getLanguage()
            + "-" + LocaleContextHolder.getLocale().getCountry();

Solution 7 - Java

You can enforce VM arguments for a JAR file with the following code:

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class JVMArgumentEnforcer
{
	private String argument;

	public JVMArgumentEnforcer(String argument)
	{
		this.argument = argument;
	}

	public static long getTotalPhysicalMemory()
	{
		com.sun.management.OperatingSystemMXBean bean =
				(com.sun.management.OperatingSystemMXBean)
						java.lang.management.ManagementFactory.getOperatingSystemMXBean();
		return bean.getTotalPhysicalMemorySize();
	}

	public static boolean isUsing64BitJavaInstallation()
	{
		String bitVersion = System.getProperty("sun.arch.data.model");

		return bitVersion.equals("64");
	}

	private boolean hasTargetArgument()
	{
		RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
		List<String> inputArguments = runtimeMXBean.getInputArguments();

		return inputArguments.contains(argument);
	}

	public void forceArgument() throws Exception
	{
		if (!hasTargetArgument())
		{
			// This won't work from IDEs
			if (JARUtilities.isRunningFromJARFile())
			{
				// Supply the desired argument
				restartApplication();
			} else
			{
				throw new IllegalStateException("Please supply the VM argument with your IDE: " + argument);
			}
		}
	}

	private void restartApplication() throws Exception
	{
		String javaBinary = getJavaBinaryPath();
		ArrayList<String> command = new ArrayList<>();
		command.add(javaBinary);
		command.add("-jar");
		command.add(argument);
		String currentJARFilePath = JARUtilities.getCurrentJARFilePath();
		command.add(currentJARFilePath);

		ProcessBuilder processBuilder = new ProcessBuilder(command);
		processBuilder.start();

		// Kill the current process
		System.exit(0);
	}

	private String getJavaBinaryPath()
	{
		return System.getProperty("java.home")
				+ File.separator + "bin"
				+ File.separator + "java";
	}

	public static class JARUtilities
	{
		static boolean isRunningFromJARFile() throws URISyntaxException
		{
			File currentJarFile = getCurrentJARFile();

			return currentJarFile.getName().endsWith(".jar");
		}

		static String getCurrentJARFilePath() throws URISyntaxException
		{
			File currentJarFile = getCurrentJARFile();

			return currentJarFile.getPath();
		}

		private static File getCurrentJARFile() throws URISyntaxException
		{
			return new File(JVMArgumentEnforcer.class.getProtectionDomain().getCodeSource().getLocation().toURI());
		}
	}
}

It is used as follows:

JVMArgumentEnforcer jvmArgumentEnforcer = new JVMArgumentEnforcer("-Duser.language=pt-BR"); // For example
jvmArgumentEnforcer.forceArgument();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMozenRathView Question on Stackoverflow
Solution 1 - JavalucasmoView Answer on Stackoverflow
Solution 2 - JavaalerootView Answer on Stackoverflow
Solution 3 - JavaAravind YarramView Answer on Stackoverflow
Solution 4 - JavaAntônio MedeirosView Answer on Stackoverflow
Solution 5 - JavaRyan OuyangView Answer on Stackoverflow
Solution 6 - JavaNick BorgesView Answer on Stackoverflow
Solution 7 - JavaBullyWiiPlazaView Answer on Stackoverflow