How do I set the default locale in the JVM?
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 theuser.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.
Setting java locale settings
I believe java gleans this from the environment variables in which it was launched, so you'll need to make sure your LANG and LC_* environment variables are set appropriately.
The locale manpage has full info on said environment variables.
How to change the locale for JVM?
To make this work, you have to tell the Scanner object to ignore the user's locale when deciding how to parse numbers.
public static double[] arrayFromUrl(String url) throws Exception {
Scanner fin = new Scanner((new URL(url)).openStream());
fin.useLocale(Locale.US);
int count = fin.nextInt();
double[] dubs = new double[count];
for ( int i=0; i<dubs.length; i++)
dubs[i] = fin.nextDouble();
fin.close();
return dubs;
}
It is also possible to change the system locale for the whole JVM by adding the following flags to the java command line:
java -Duser.language=en -Duser.region=US MyClass
Locale.getDefault() returns en always
In Linux/Unix/Mac, the settings LC_ALL
and LANG
can control the default locale for Java programs. In Windows, the locales are set from the Control Panel, under Regional and Language Options.
When the JVM starts in a *nix environment, it will do this:
- Scan the environment for
LC_ALL
- If
LC_ALL
doesn't exist, scan the environment forLANG
- If the JVM setting
user.language
is set, use that in place of the environment variables. - If nothing is set, default to
en_US
(I believe this is the final failure case)
In your environment, you have LC_ALL
set to C
, which is just the C locale. It's basically a traditional fallback to the days when locales weren't used.
You can change LC_ALL
in your case, and restart your JVM, and you should get a new value for java.util.Locale.getDefault()
.
Example:
import java.util.Locale;
public class LocaleTest {
public static void main(String[] args) {
System.out.println(Locale.getDefault());
}
}
Here's running:
> LC_ALL=en_UK java LocaleTest
en_UK
> LC_ALL=ja_JP java LocaleTest
ja_JP
Also note that if you're running Java 1.7.0-b147, there is a bug with the JRE not recognizing environment settings for locale, and will always use the default system locale.
Bug report here: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073906
How do I change the default locale settings in Java to make them consistent?
Nice, i was interested about the same thing but so far I have only a partial solution:
user.timezone=UTC
user.country=CA
user.variant=
user.language=en
file.encoding=utf8
You can use Canada, because it one of the countries using the ISO 8601 format.
Related Topics
How to Turn Off the Eclipse Code Formatter for Certain Sections of Java Code
How to Deal with a Slow Securerandom Generator
How to Supply Value to an Annotation from a Constant Java
Find a Line in a File and Remove It
Regex for Matching Something If It Is Not Preceded by Something Else
Java Generics: List, List<Object>, List<>
Why Would You Ever Implement Finalize()
Is the Use of Java's Default Package a Bad Practice
Scanner VS. Stringtokenizer VS. String.Split
Why Does Inetaddress.Isreachable Return False, When I Can Ping the Ip Address
How to Make a Jar File That Includes Dll Files