Setting java locale settings
Asked Answered
C

10

40

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?

Cade answered 15/9, 2008 at 15:39 Comment(4)
Locale.getavailablelocale() method is not returning en_MY, why so? it returns 155 localesWondrous
@kekan : Try Locale.getDefault();Doggett
Beware of Java 7 default locale changes.Musket
Possible duplicate of how do I set the default locale for my JVM?Musket
H
15

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.

Heliochrome answered 15/9, 2008 at 15:42 Comment(3)
What are the LC_* environment variables ?Bonaire
If your program formats numbers, you probably want to set LC_ALL. See my answer below.Pensile
Be 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.Scribe
H
51

With the user.language, user.country and user.variant properties.

Example:

java -Duser.language=th -Duser.country=TH -Duser.variant=TH SomeClass

Houselights answered 15/9, 2008 at 15:45 Comment(2)
This 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.Ashy
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)...Notebook
P
31

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.

Pensile answered 27/3, 2012 at 17:48 Comment(0)
M
19

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.

Muscle answered 8/3, 2019 at 17:5 Comment(2)
Hmm, that gives me a "default locale", "default display locale" and "default format locale"... what the heck is the difference though?Thies
Very nice trick! Thank you for sharing! It is useful when you have distroless container with no shell and only java command.Cutter
H
15

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.

Heliochrome answered 15/9, 2008 at 15:42 Comment(3)
What are the LC_* environment variables ?Bonaire
If your program formats numbers, you probably want to set LC_ALL. See my answer below.Pensile
Be 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.Scribe
B
13

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.

Barbed answered 15/9, 2008 at 15:41 Comment(1)
The supported languages are listed at oracle.com/technetwork/java/javase/…Shemikashemite
I
12

For tools like jarsigner which is implemented in Java.

JAVA_TOOL_OPTIONS=-Duser.language=en jarsigner
Iraqi answered 28/7, 2014 at 1:44 Comment(3)
This environment works better, just add export JAVA_TOOL_OPTIONS=-Duser.language=en in your ~/.profileMarigraph
See #28328120Musket
Finally, this is the option I want! java and javac does not seem to honour LANG and LC_ALL.Subsellium
I
3

You can change on the console:

$ export LANG=en_US.utf8
Interatomic answered 18/5, 2012 at 5:0 Comment(1)
First, you need to write 'utf-8' instead 'utf8'. Second, only the encoding part works, but not the language part.Subsellium
W
2

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!!

Winniewinnifred answered 29/5, 2010 at 22:42 Comment(0)
I
1

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.

Impuissant answered 15/4, 2015 at 13:53 Comment(0)
A
0

One way to control the locale settings is to set the java system properties user.language and user.region.

Amphitrite answered 15/9, 2008 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.