Navaneeth K N
Navaneeth K N

Reputation: 15501

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 console showing scrambled text

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.

Upvotes: 4

Views: 1892

Answers (5)

grepit
grepit

Reputation: 22382

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 enter image description here

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109557

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")

Upvotes: 1

Jack Harkness
Jack Harkness

Reputation: 810

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.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

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.

enter image description here

Upvotes: 2

assylias
assylias

Reputation: 328618

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.

Upvotes: 3

Related Questions