Windows 10 CLI UTF-8 encoding

℡╲_俬逩灬. 提交于 2019-12-10 10:36:49

问题


Problem:

On an english Windows 10 using slovenian keyboard layout, all command line interfaces seem to have a problem with displaying (printing) UTF-8 characters, namely č, š and ž, which are replaced with ?. (I assume all UTF-8 specific characters, since ć and đ also do not work. )

Tested in:

  • CMD, Powershell, Cmder on Windows 10 64-bit English - Slovenian keyboard layout ... unsuccessful
  • Intellij IDEA on Windows 10 64-bit English language - Slovenian keyboard layout ... successful -> Works as needed in IDE, but not CLI.
  • CMD Windows 10 64-bit English language - English keyboard ... successful
  • CMD Windows 10 64-bit Slovenian languge - Slovenian keyboard layout ... succesful
  • Several distros of Linux (Ubuntu, Mint, Kali) ... successful

Tried so far:

  • changing chcp to chcp 65001 ... unsuccessful
  • creating Autorun file in regedit to force UTF-8 ... unsuccessful
  • different java compilers ... unsuccessful

Sample code:

public class Test2 {
public static void main(String[] args) {
    System.out.println("č š ž ć đ");

    }
}

CMD:

>javac -encoding UTF-8 test2.java
>java Test2
? ? ? ? ? 

Other notes:

Problem appears on several computers running on different hardware. All of the above mentioned characters work fine in all of the above mentioned CLI by default. So the problem only seems to appear with java.


回答1:


Use chcp 65001 then run with java -Dfile.encoding=UTF-8 Test2:

chcp 65001
javac -encoding UTF-8 Test2.java
java -Dfile.encoding=UTF-8 Test2

Remember to name your Java source file after the class name, case-sensitive.




回答2:


After following @Andreas advice, i have further explored the issue and found a fix that works:

First force cmd to use chcp 65001 (UTF-8) following this link on superuser.

Secondly use the following command:

set JAVA_TOOL_OPTIONS =-Dfile.encoding=UTF-8


来源:https://stackoverflow.com/questions/49016189/windows-10-cli-utf-8-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!