简体   繁体   中英

Why is this Java program gives incorrect results on Eclipse and correct results when run from terminal?

Consider the following program.

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class HelloWorld {

    public static void main(String[] args)  {       
        System.out.println(Charset.defaultCharset());
        char[] array = new char[3];
        array[0] = '\u0905';
        array[1] = '\u0905';
        array[2] = '\u0905';
        CharBuffer charBuffer = CharBuffer.wrap(array);
        Charset utf8 = Charset.forName("UTF-8");
        ByteBuffer encoded = utf8.encode(charBuffer);
        System.out.println(new String(encoded.array()));

    }
}

When I execute this using terminal,

java HelloWorld

I get properly encoded, shaped text. Default encoding was MacRoman .

Now when I execute the same code from Eclipse, I see incorrect text getting printed to the console.

Eclipse控制台显示混乱的文本

When I change the file encoding option of Eclipse to UTF-8 , it prints correct results in Eclipse.

I am wondering why this happens? Ideally, file encoding options should not have affected this code because here I am using UTF-8 explicitly.

Any idea why this is happening?

I am using Java 1.6 (Sun JDK), Mac OSx 10.7.

You need to specify what encoding you want to use when creating the string:

new String(encoded.array(), charset)

otherwise it will use the default charset.

Make sure the console you use to display the output is also encoded in UTF-8. In Eclipse for example, you need to go to Run Configuration > Common to do this.

在此处输入图片说明

System.out.println("\u0905\u0905\u0905");

would be the straight-forward usage.

And encoding is missing for the String constructor, defaulting to the set default encoding.

new String(encoded.array(), "UTF-8")

This happens because Eclipse uses the default ANSI encoding, not UFT-8. If your using a different encoding than what your IDE is using, you will get unreadable results.

you need to change your console run configuration.

  • click on "Run"
  • click on "Run Configurations" and then click on "common" tab
  • change Encoding to UTF 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM