关于Java:如何为JVM设置默认语言环境?

how do I set the default locale for my JVM?

我想将我的JVM的默认语言环境设置为fr_CA。 有哪些可能的选择方法?

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


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

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.


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

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

有关更多信息,请参见国际化:了解Java平台中的语言环境-使用语言环境


您可以使用JVM args

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

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

  • 以编程方式,使用Locale.setDefault()(在我的情况下,这是解决方案,因为我不想要求用户执行任何操作):

    1
    Locale.setDefault(new Locale("pt","BR"));
  • 通过JVM的参数:

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

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

Note: You must be logged in with an account that has Administrative Privileges.

  • Click Start > Control Panel.

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

  • 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.)

  • Under the Language for non-Unicode programs section, select the desired language from the drop down menu.

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

  • Follow the guided instructions to install the files.

  • Restart the computer after the installation is complete.

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


    如果您不想更改系统区域设置,但可以更改JVM,那么还有另一件事。您可以设置系统(或用户)环境变量JAVA_TOOL_OPTIONS并将其值设置为-Duser.language=en-US或所需的任何其他语言区域。


    你可以这样做:

    enter image description here

    enter image description here

    并捕获语言环境。你可以这样做:

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


    您可以使用以下代码为JAR文件实施VM参数:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    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());
            }
        }
    }

    它的用法如下:

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