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?

10 Answers10

51

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

Example:

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

Chris Broadfoot
  • 5,082
  • 1
  • 29
  • 37
  • 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. – 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
30

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.

cayhorstmann
  • 3,192
  • 1
  • 25
  • 17
16

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.

Matthew Buckett
  • 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
  • 1
    Very 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
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.

pjz
  • 41,842
  • 6
  • 48
  • 60
  • What are the LC_* environment variables ? – Leonel Jul 29 '10 at 14:32
  • If your program formats numbers, you probably want to set LC_ALL. See my answer below. – cayhorstmann Mar 27 '12 at 17:49
  • 1
    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`. – Gunnar Aug 24 '17 at 12:48
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.

ecruz3
  • 13
  • 3
sblundy
  • 60,628
  • 22
  • 121
  • 123
  • 2
    The supported languages are listed at http://www.oracle.com/technetwork/java/javase/locales-137662.html#translation – koppor Jan 04 '13 at 11:48
12

For tools like jarsigner which is implemented in Java.

JAVA_TOOL_OPTIONS=-Duser.language=en jarsigner
hiroshi
  • 6,871
  • 3
  • 46
  • 59
3

You can change on the console:

$ export LANG=en_US.utf8
Igor
  • 33,276
  • 14
  • 79
  • 112
Derzu
  • 7,011
  • 3
  • 57
  • 60
  • 2
    First, 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
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!!

Gmu
  • 368
  • 2
  • 12
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.

aseychell
  • 1,794
  • 17
  • 35
0

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

killdash10
  • 706
  • 5
  • 12