Java,Ant 错误:编码 Cp1252 的字符不可映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23399865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java, Ant error: unmappable character for encoding Cp1252
提问by user2666282
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"
我在我的项目中使用 Java、Eclipse 和 Ant。我有一些 Java 代码需要编辑并在其中添加一些 UTF-8 字符。以前我的 build.xml 有:而且它工作正常。现在,当我尝试运行时添加这些 UTF-8 字符后,它会抛出“错误:编码 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.
谁能告诉我什么是修复?我尝试将 xml 中的编码更改为 UTF-8 和 Cp1252,但没有成功。
I'm using JRE7, Eclipse Kepler and Ant 4.11.
我正在使用 JRE7、Eclipse Kepler 和 Ant 4.11。
采纳答案by Matthew M
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">
这可能很棘手,简单地更改“广告”编码并不能弥补文件中存在无法使用 UTF-8 解释理解的字节的事实。在 Ant 中,您需要更新javac
任务以添加编码,例如,<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.
确保 Eclipse 中的文件编码也是 UTF-8,因为某些 cp1252 字符也不会直接映射到 UTF-8。您可能希望使用单一编码来维护整个项目。否则,当编译器只需要一种编码时,它就会看到不同的编码。
回答by Andrea Luciano
You can try to set the environment variable called ANT_OPTS (or JAVA_TOOL_OPTIONS) to -Dfile.encoding=UTF8
您可以尝试将名为 ANT_OPTS(或 JAVA_TOOL_OPTIONS)的环境变量设置为 -Dfile.encoding=UTF8
回答by false9striker
Go to common tab of RUN/DEBUG configuration in eclipse change encoding to UTF-8.
转到 Eclipse 中 RUN/DEBUG 配置的公共选项卡,将编码更改为 UTF-8。
回答by Gopi
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.
在我的一个项目中遇到了类似的问题。我的一些文件有 UTF-8 字符,并且由于 Eclipse 默认编码 - cp1252,构建失败并出现此错误。
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"
- 将eclipse项目级别的编码更改为UTF-8(项目属性->“文本文件编码”->选择“其他”选项->从下拉列表中选择“UTF-8”)
- 在 ant 构建脚本中为 javac 任务添加编码属性,值为“UTF-8”
Set the encoding type according to the special characters used in your code/files.
根据您的代码/文件中使用的特殊字符设置编码类型。
回答by StinkyMadness
Window > Preferences > General > Content Types, set UTF-8 as the default encoding for all content types.
Window > Preferences > General > Content Types,将 UTF-8 设置为所有内容类型的默认编码。
Window > Preferences > General > Workspace, set "Text file encoding" to "Other : UTF-8".
Window > Preferences > General > Workspace,将“文本文件编码”设置为“其他:UTF-8”。