- 1 year ago
Cannot display an unicode character on windows cmd using Java
To display Unicode characters properly in the Windows command prompt (cmd) using Java, you need to ensure that the correct character encoding is used. By default, the Windows command prompt uses the "cp437" encoding, which does not support all Unicode characters.
To display Unicode characters in Java on Windows cmd, you can try the following steps:
Set the encoding of the command prompt to UTF-8:
- Open the command prompt.
- Run the following command:
chcp 65001
- This sets the command prompt encoding to UTF-8.
Use the correct encoding in your Java code:
- Before displaying Unicode characters, set the encoding to UTF-8 using
System.setProperty("file.encoding", "UTF-8");
- For example:java
public class Main {
public static void main(String[] args) {
System.setProperty("file.encoding", "UTF-8");
System.out.println("Hello, 你好, Привет");
}
}
- Before displaying Unicode characters, set the encoding to UTF-8 using
Compile and run your Java code in the command prompt:
- Compile your Java code using
javac
. - Run your Java program using
java
. - For example:css
javac Main.java
java Main
- Compile your Java code using
With these steps, the Unicode characters should be displayed correctly in the Windows command prompt when running your Java program. Note that this approach requires the command prompt to be set to UTF-8 encoding, and it may not work in all environments or versions of Windows cmd.