# Troubleshooting Guide for Encoding Issues This document provides a guide mostly for Windows users to solve common Java encoding issues. ## 1. Background Computers can only understand the binary data such as 0 and 1, and it uses charset to encode/decode the data into real-world characters. When two processes interact with each other for I/O, they have to use the compatible charset for encoding and decoding, otherwise garbled characters will probably appear. macOS and Linux use UTF-8 everywhere and encoding is not a problem for them. For Windows, however, the default charset is not UTF-8 and is platform-dependent, which can lead to inconsistent encoding between different tools. ## 2. Common Problems Below are the typical encoding problems when running a Java program on Windows terminal. 2.1) The file or directory name contains unicode characters, Java launcher cannot find the corresponding classpath or main class well. ``` C:\Test>java -cp 中文目录 Hello Error: Could not find or load main class Hello ``` ``` C:\Test>java -cp ./Exercises 练习 Error: Could not find or load main class ?? Caused by: java.lang.ClassNotFoundException: ?? ``` 2.2) The string literals with unicode characters appear garbled when printed to the terminal. ```java public class Hello { public static void main(String[] args) { System.out.println("你好!"); } } ``` ``` C:\Test>java -cp ./Exercises Hello ??! ``` 2.3) Garbled characters when Java program interacts with terminal for I/O. ```java import java.util.Scanner; public class Hello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println(scanner.nextLine()); } } ``` ``` C:\Test>chcp 65001 C:\Test>java -Dfile.encoding=UTF-8 -cp ./Exercises Hello 你好 �� ``` ## 3.Troubleshooting Suggestions The following diagram shows the parts of encoding that may be involved when writing and running Java in VS Code.
Encoding on Windows
Change System Locale