22

I am using Java, Eclipse and Ant in my project. I had some Java code that I needed to edit and add some UTF-8 chars in them. Previously my build.xml had: And it worked fine. Now after adding those UTF-8 chars when I try to run, it throws "error: unmappable character for encoding Cp1252"

Could anyone please tell me what is the fix? I tried changing the encoding to UTF-8 and Cp1252 in the xml but with no luck.

I'm using JRE7, Eclipse Kepler and Ant 4.11.

CJBS
  • 15,147
  • 6
  • 86
  • 135
user2666282
  • 381
  • 1
  • 2
  • 15
  • 1
    The problem should be solved in four places simultaneously: http://stackoverflow.com/a/34225997/715269 – Gangnus Dec 11 '15 at 14:40
  • I had the same problem. None of the solutions below worked. I created a new file with the right encoding and then copied and pasted the contents of the class (.java). It worked for me. Previous to that the unknown character kept reinserting itself when I run ant, event when I explicitly deleted it and saved the file. Weird right? – 1vand1ng0 Sep 03 '17 at 16:14

5 Answers5

21

This can be tricky simply changing the "advertised" encoding does not make up for the fact that there are bytes in the file that cannot be understood using a UTF-8 interpretation. In Ant you will need to update the javac task to add an encoding like, <javac ... encoding="utf-8">

Make sure that the file encoding in Eclipse is also UTF-8 because some cp1252 characters do not directly map into UTF-8 either. You will probably want to maintain your entire project using a single encoding. Otherwise the compiler will be seeing different encodings when it only expects one.

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
Matthew M
  • 1,155
  • 8
  • 9
11

You can try to set the environment variable called ANT_OPTS (or JAVA_TOOL_OPTIONS) to -Dfile.encoding=UTF8

Kelly Elton
  • 4,373
  • 10
  • 53
  • 97
Andrea Luciano
  • 461
  • 3
  • 5
4

Had the similar issue in one of my projects. Some of my files had UTF-8 characters and due to eclipse default encoding - cp1252, build failed with this error.

To resolve the issue, follow the below steps -

  1. Change the encoding at eclipse project level to UTF-8 (Project properties -> "Text file encoding" -> select "Other" option -> select "UTF-8" from the drop down)
  2. Add encoding attribute for javac task in ant build script with value "UTF-8"

Set the encoding type according to the special characters used in your code/files.

Gopi
  • 79
  • 6
1

Go to common tab of RUN/DEBUG configuration in eclipse change encoding to UTF-8.

Change encoding

Venugopal Madathil
  • 2,031
  • 3
  • 34
  • 44
0

Window > Preferences > General > Content Types, set UTF-8 as the default encoding for all content types.

Window > Preferences > General > Workspace, set "Text file encoding" to "Other : UTF-8".