Java, Ant error: unmappable character for encoding Cp1252
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 -
- 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)
- 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.
You can try to set the environment variable called ANT_OPTS (or JAVA_TOOL_OPTIONS) to -Dfile.encoding=UTF8
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.