设置系统编码为UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
或修改 /etc/default/locale
文件,确保内容为:LANG="en_US.UTF-8"
LC_ALL="en_US.UTF-8"
然后重启系统或执行 source /etc/default/locale
。指定Java编译器编码
-encoding UTF-8
参数:javac -encoding UTF-8 YourJavaFile.java
若需永久生效,可将该参数添加到 ~/.bashrc
中:echo "alias javac='javac -encoding UTF-8'" >> ~/.bashrc
source ~/.bashrc
检查Java文件编码
file -i YourJavaFile.java
命令验证。配置Java虚拟机编码
System.setProperty("file.encoding", "UTF-8");
或通过JVM参数设置:java -Dfile.encoding=UTF-8 YourJavaClass
安装中文字体(解决显示问题)
sudo apt-get install ttf-wqy-zenhei
清理并重新编译
.class
文件后重新编译:rm *.class
javac -encoding UTF-8 YourJavaFile.java
验证:编写包含中文的测试程序,运行后若终端输出正常,则问题解决。
示例代码:
public class TestEncoding {
public static void main(String[] args) {
System.out.println("你好,世界!");
}
}