3

Hi so I am on Win10 and Git 2.35.1.

$ git --version
git version 2.35.1.windows.1

I think my Git Bash terminal has some encoding issues. For example if I type java the message I got is all mojibake.

(I am not sure why my Java is printing in another language other than English honestly. I did not configure anything special when I install JRE or JDK.)

$ java
▒÷▒: java [-options] class [args...]
           (ִ▒▒▒▒)
   ▒▒  java [-options] -jar jarfile [args...]
           (ִ▒▒ jar ▒ļ▒)
▒▒▒▒ѡ▒▒▒▒▒:
    -d32          ʹ▒▒ 32 λ▒▒▒▒ģ▒▒ (▒▒▒▒▒▒▒)
    -d64          ʹ▒▒ 64 λ▒▒▒▒ģ▒▒ (▒▒▒▒▒▒▒)
    -client       ѡ▒▒ "client" VM
    -server       ѡ▒▒ "server" VM
                  Ĭ▒▒ VM ▒▒ client.

My locale is UTF-8 though:

$ locale
LANG=
LC_CTYPE="C.UTF-8"
LC_NUMERIC="C.UTF-8"
LC_TIME="C.UTF-8"
LC_COLLATE="C.UTF-8"
LC_MONETARY="C.UTF-8"
LC_MESSAGES="C.UTF-8"
LC_ALL=C.UTF-8

And, when using commands like echo, UTF-8 characters are correctly displayed though:

$ echo 中文中文
中文中文

I tried various methods like git config --global i18n.logOutputEncoding utf-8 and setx LC_ALL C.UTF-8 but java command still prints a mess.

What is the problem?

Is java command using some other encoding all together, not UTF-8?

torek
  • 448,244
  • 59
  • 642
  • 775
Reimirno
  • 148
  • 1
  • 9
  • 1
    You can switch locale permanently as described here: https://stackoverflow.com/questions/6217297/forcing-the-use-of-english-in-jdk7-tools – orhtej2 Apr 17 '22 at 22:54
  • Our use terminal emulator that better handles utf8 – orhtej2 Apr 17 '22 at 22:55
  • Can you paste first line of `java |& xxd` ? – Philippe Apr 18 '22 at 00:08
  • @Philippe `00000000: d3c3 b7a8 a3ba 6a61 7661 205b 6f70 7469 ......java [opti` – Reimirno Apr 18 '22 at 22:33
  • @user16320675 On Windows CMD java prints Chinese Characters correctly. `C:\Windows\System32>java 用法:java [options] <主类> [args...] (执行类) 或 java [options] -jar [args...] (执行 jar 文件)` and so on. Only Git Bash screws up the encoding. – Reimirno Apr 18 '22 at 22:35
  • @orhtej2 Nice. Found a solution that works by following your link. – Reimirno Apr 18 '22 at 22:46

2 Answers2

2

Check first, as in here, if this works:

java -Duser.language=en -Duser.country=US

java -Duser.language=en -Duser.country=US -XshowSettings -version

The second command would show the current settings.

If that work, you can set those options through the environment variable JAVA_TOOL_OPTIONS.
Although, since JDK9+, JDK_JAVA_OPTIONS is a valid alternative.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Not working. The first line seems to be an incorrect usage of `java` command as when I type it into bash, it prints the help message of `java` command. – Reimirno Apr 18 '22 at 22:32
  • That seems expected, since the java command is not followed by any other subcommands. Does it print the help message entirety in English though? Or, as in your question, does it still mix languages? – VonC Apr 18 '22 at 22:40
  • Yes. If I append `-Duser.language=en -Duser.country=US` to a java command then the log will be printed in English. However you must do this everytime. – Reimirno Apr 18 '22 at 22:44
  • 1
    @LongYu Excellent, I was waiting that confirmation to suggest `JAVA_TOOL_OPTIONS`. I have edited the answer accordingly. – VonC Apr 19 '22 at 05:58
2

Following instructions here, I found a permanent fix: REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v JAVA_TOOL_OPTIONS /d -Duser.language=en /t REG_SZ /f and restart computer.

And now when I type java

$ java
Picked up JAVA_TOOL_OPTIONS: -Duser.language=en
Usage: java [options] <mainclass> [args...]
           (to execute a class)
   or  java [options] -jar <jarfile> [args...]
           (to execute a jar file)
   or  java [options] -m <module>[/<mainclass>] [args...]
...
Reimirno
  • 148
  • 1
  • 9