如何设置JVM的默认语言环境?

时间:2021-05-11 00:21:57

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

我想将我的JVM的默认语言环境设置为fr_CA。有什么可能的选择呢?

I know of only one option Locale.setDefault()

我只知道一个选项Locale.setDefault()

5 个解决方案

#1


121  

From the Oracle Reference:

来自Oracle参考:

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.

应用程序的默认语言环境以三种方式确定。首先,除非您明确更改了默认值,否则Locale.getDefault()方法将返回Java虚拟机(JVM)在首次加载时最初确定的区域设置。也就是说,JVM从主机环境确定默认语言环境。主机环境的区域设置由主机操作系统和在该系统上建立的用户首选项确定。

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.

其次,在某些Java运行时实现上,应用程序用户可以通过设置user.language,user.country和user.variant系统属性在命令行上提供此信息来覆盖主机的默认语言环境。

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.

第三,您的应用程序可以调用Locale.setDefault(Locale)方法。 setDefault(Locale aLocale)方法允许您的应用程序设置系统范围(实际上是VM范围的)资源。使用此方法设置默认语言环境后,对Locale.getDefault()的后续调用将返回新设置的语言环境。

#2


113  

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

您可以通过JVM参数在命令行上进行设置:

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

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

有关更多信息,请参阅国际化:了解Java平台中的区域设置 - 使用区域设置

#3


32  

You can use JVM args

您可以使用JVM args

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

#4


18  

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

在这里的答案中,到目前为止,我们找到了两种更改JRE语言环境设置的方法:

  • 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()(在我的情况下,这是解决方案,因为我不想要求用户的任何操作):

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

    通过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.

但是,作为参考,我想要注意的是,在Windows上,还有一种更改JRE使用的语言环境的方法,如下所述:更改系统范围的语言。

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 7和Vista:单击时钟,语言和区域>区域和语言。

    Windows XP: Double click the Regional and Language Options icon.

    Windows XP:双击“区域和语言选项”图标。

    The Regional and Language Options dialog box appears.

    将出现“区域和语言选项”对话框。

  3. Windows 7: Click the Administrative tab.

    Windows 7:单击“管理”选项卡。

    Windows XP and Vista: Click the Advanced tab.

    Windows XP和Vista:单击“高级”选项卡。

    (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.

    在“非Unicode程序的语言”部分下,从下拉菜单中选择所需的语言。

  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.

    系统显示一个对话框,询问是使用现有文件还是从操作系统CD安装。确保准备好CD。

  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.

当然在Linux上,JRE还使用系统设置来确定要使用的语言环境,但是用于设置从发行版到发行版的系统范围语言更改的说明。

#5


1  

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

您可以使用以下代码为JAR文件强制执行VM参数:

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();

#1


121  

From the Oracle Reference:

来自Oracle参考:

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.

应用程序的默认语言环境以三种方式确定。首先,除非您明确更改了默认值,否则Locale.getDefault()方法将返回Java虚拟机(JVM)在首次加载时最初确定的区域设置。也就是说,JVM从主机环境确定默认语言环境。主机环境的区域设置由主机操作系统和在该系统上建立的用户首选项确定。

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.

其次,在某些Java运行时实现上,应用程序用户可以通过设置user.language,user.country和user.variant系统属性在命令行上提供此信息来覆盖主机的默认语言环境。

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.

第三,您的应用程序可以调用Locale.setDefault(Locale)方法。 setDefault(Locale aLocale)方法允许您的应用程序设置系统范围(实际上是VM范围的)资源。使用此方法设置默认语言环境后,对Locale.getDefault()的后续调用将返回新设置的语言环境。

#2


113  

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

您可以通过JVM参数在命令行上进行设置:

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

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

有关更多信息,请参阅国际化:了解Java平台中的区域设置 - 使用区域设置

#3


32  

You can use JVM args

您可以使用JVM args

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

#4


18  

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

在这里的答案中,到目前为止,我们找到了两种更改JRE语言环境设置的方法:

  • 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()(在我的情况下,这是解决方案,因为我不想要求用户的任何操作):

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

    通过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.

但是,作为参考,我想要注意的是,在Windows上,还有一种更改JRE使用的语言环境的方法,如下所述:更改系统范围的语言。

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 7和Vista:单击时钟,语言和区域>区域和语言。

    Windows XP: Double click the Regional and Language Options icon.

    Windows XP:双击“区域和语言选项”图标。

    The Regional and Language Options dialog box appears.

    将出现“区域和语言选项”对话框。

  3. Windows 7: Click the Administrative tab.

    Windows 7:单击“管理”选项卡。

    Windows XP and Vista: Click the Advanced tab.

    Windows XP和Vista:单击“高级”选项卡。

    (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.

    在“非Unicode程序的语言”部分下,从下拉菜单中选择所需的语言。

  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.

    系统显示一个对话框,询问是使用现有文件还是从操作系统CD安装。确保准备好CD。

  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.

当然在Linux上,JRE还使用系统设置来确定要使用的语言环境,但是用于设置从发行版到发行版的系统范围语言更改的说明。

#5


1  

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

您可以使用以下代码为JAR文件强制执行VM参数:

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();