Search code examples
javautf-8encodecyrillic

URLEncoder Cyrillic encode UTF-8 bad result


String text = URLEncoder.encode("кот", "UTF-8");
System.out.println("\nEncoding result : " + text);

Encoding result : %D0%A0%D1%94%D0%A0%D1%95%D0%A1%E2%80%9A

But I excpect: %D0%BA%D0%BE%D1%82 (looking here http://meyerweb.com/eric/tools/dencoder/)

What do I have to do to get %D0%BA%D0%BE%D1%82 as the result?

  • My file is UTF-8 without BOM.
  • If i make it ANSI - all is OK.
  • BUT! How i can force it to work with UTF-8 without BOM?

Solution

  • Your IDE or editor is displaying your source file as UTF-8, but the javac compiler is probably using a default encoding according to your OS regional setting. Use the option javac -encoding utf-8 to override that.