When I use the default java locale on my linux machine it comes out with the US locale settings, where do I change this so that it comes out with the correct locale?
-
Locale.getavailablelocale() method is not returning en_MY, why so? it returns 155 locales – VedantK Jul 07 '15 at 10:19
-
@kekan : Try Locale.getDefault(); – Michael Fayad Mar 08 '16 at 16:00
-
Beware of [Java 7 default locale changes](http://stackoverflow.com/questions/7107972/java-7-default-locale). – Vadzim Nov 22 '16 at 09:20
-
Possible duplicate of [how do I set the default locale for my JVM?](http://stackoverflow.com/questions/8809098/how-do-i-set-the-default-locale-for-my-jvm) – Vadzim Nov 22 '16 at 09:22
10 Answers
With the user.language
, user.country
and user.variant
properties.
Example:
java -Duser.language=th -Duser.country=TH -Duser.variant=TH SomeClass

- 5,082
- 1
- 29
- 37
-
2This will work, but it's safer and more future-proof to set the environment and let the JRE figure out the correct values for these. – Air May 09 '12 at 14:42
-
The problem is that it is not working @Air... My Mac JDK environment is not able to connect any system environment variable to the locale... Only using props (JVM properties)... – czupe Mar 21 '23 at 21:19
I had to control this in a script that ran on a machine with French locale, but a specific Java program had to run with en_US. As already pointed out, the following works:
java -Duser.language=en -Duser.country=US ...
Alternatively,
LC_ALL=en_US.UTF-8 java ...
I prefer the latter.

- 3,192
- 1
- 25
- 17
If you ever want to check what locale or character set java is using this is built into the JVM:
java -XshowSettings -version
and it will dump out loads of the settings it's using. This way you can check your LANG
and LC_*
values are getting picked up correctly.

- 4,073
- 1
- 36
- 28
-
Hmm, that gives me a "default locale", "default display locale" and "default format locale"... what the heck is the difference though? – Nyerguds Aug 07 '19 at 10:20
-
1Very nice trick! Thank you for sharing! It is useful when you have distroless container with no shell and only java command. – daitangio Nov 15 '22 at 10:35
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.

- 41,842
- 6
- 48
- 60
-
-
If your program formats numbers, you probably want to set LC_ALL. See my answer below. – cayhorstmann Mar 27 '12 at 17:49
-
1Be aware that this is incomplete. I'm running macOS Sierra and have LANG as well as LC_ALL set as "en_US.UTF8". However, for some reason Java comes up with `user.language=en`, `user.country=US` and `user.country.format=DE`. – Gunnar Aug 24 '17 at 12:48
You could call during init or whatever Locale.setDefault() or -Duser.language=, -Duser.country=, and -Duser.variant= at the command line. Here's something on Sun's site.
-
2The supported languages are listed at http://www.oracle.com/technetwork/java/javase/locales-137662.html#translation – koppor Jan 04 '13 at 11:48
For tools like jarsigner
which is implemented in Java.
JAVA_TOOL_OPTIONS=-Duser.language=en jarsigner

- 6,871
- 3
- 46
- 59
-
2This environment works better, just add export JAVA_TOOL_OPTIONS=-Duser.language=en in your ~/.profile – Jiejing Zhang Oct 21 '14 at 02:42
-
See http://stackoverflow.com/questions/28327620/difference-between-java-options-java-tool-options-and-java-opts/ – Vadzim Nov 22 '16 at 09:17
-
Finally, this is the option I want! java and javac does not seem to honour LANG and LC_ALL. – Yongwei Wu Dec 07 '16 at 04:57
-
2First, you need to write 'utf-8' instead 'utf8'. Second, only the encoding part works, but not the language part. – Yongwei Wu Dec 07 '16 at 04:59
If you are on Mac, simply using System Preferences -> Languages and dragging the language to test to top (before English) will make sure the next time you open the App, the right locale is tried!!

- 368
- 2
- 12
On linux, create file in /etc/default/locale
with the following contents
LANG=en.utf8
and then use the source
command to export this variable by running
source /etc/default/locale
The source command sets the variable permanently.

- 1,794
- 17
- 35
One way to control the locale settings is to set the java system properties user.language and user.region.

- 706
- 5
- 12