How do I set the default locale in the JVM?

前端 未结 7 1186
半阙折子戏
半阙折子戏 2020-11-22 11:42

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

7条回答
  •  礼貌的吻别
    2020-11-22 11:55

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

提交回复
热议问题