开发者

Java unicode characters error in cmd

开发者 https://www.devze.com 2023-01-08 21:26 出处:网络
I have the following class in Java which prints \"Hello World\" in portuguese: public class PrintUnicode {

I have the following class in Java which prints "Hello World" in portuguese:

public class PrintUnicode {
    public static void main(String[] args) {
        开发者_如何转开发System.out.println("Olá Mundo!");
    }
}

I am using Eclipse, so I exported the project to a Runnable Jar File. After that, I went to cmd (Windows 7) and ran the generated jar file.

The result was:

Olß Mundo!

Is there an easy way to avoid this error?


Found the solution. Just change to:

public class PrintUnicode {
    public static void main(String[] args) {
        System.console().printf("Olá Mundo!");
    }
}

The error with System.out happens because:

By default, Java encodes Strings sent to System.out in the default code page. On Windows XP, this means a lossy conversion to an "ANSI" code page. This is unfortunate, because the Windows Command Prompt (cmd.exe) can read and write Unicode characters. (source here)

0

精彩评论

暂无评论...
验证码 换一张
取 消