Java Windows 上的 Ant 编码问题 - UTF-8 文件但在变音符号上吐出垃圾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4211825/
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
Ant encoding problem on Windows - UTF-8 files but spits garbage on diacritics
提问by davidcesarino
Somehow I can't get my UTF-8 sources to play nice with Ant.
不知何故,我无法让我的 UTF-8 源代码与 Ant 配合得很好。
I get a whole lot of "warning: unmappable character for encoding ascii". I'm going crazy, really. Hours and hours and hours. Btw, I noticed 5 people already used the tag crazy. :-)
我收到了很多“警告:编码 ascii 的不可映射字符”。我快疯了,真的。小时和小时和小时。顺便说一句,我注意到 5 个人已经疯狂地使用了这个标签。:-)
And yes, I've read this, thisand others. Google also (the first 5 page results of at least 3 or 4 different searches, at least). There are javac options. I've tried. There is also some preset or something (sorry, 3 AM). Didn't work either.
是的,我读过这个、这个和其他的。谷歌也是(至少 3 或 4 个不同搜索的前 5 页结果,至少)。有 javac 选项。我试过了。还有一些预设什么的(对不起,凌晨3点)。也没有用。
I'm generating Android apk files with Ant. I can't use Eclipse, so no. And by the way, the ant documentation is gibberish to me. Those examples are of no use at all. I've lost count on how much I've tried.
我正在用 Ant 生成 Android apk 文件。我不能使用 Eclipse,所以不能。顺便说一句,蚂蚁文档对我来说是胡言乱语。这些例子根本没有用。我已经数不清我尝试了多少。
I've tried using the Dfile.encoding option, tried mixing that with CHCP 65001 Windows command. Did all the combinations, and it even makes Ant (Javac I guess) stop spitting errors, but it still doesn't matter. My code still ends up with garbage carachters (a bunch of ?? instead of á, í etc).
我尝试使用 Dfile.encoding 选项,尝试将其与 CHCP 65001 Windows 命令混合使用。做了所有的组合,它甚至让 Ant(我猜是 Javac)停止吐出错误,但它仍然无关紧要。我的代码仍然以垃圾字符结尾(一堆 ?? 而不是 á、í 等)。
采纳答案by davidcesarino
To all you who use UTF-8 source files on Windows and was trying to build Android packages with Proguard using the famous blog post by Dan Galpin/Tim Bray.
致所有在 Windows 上使用 UTF-8 源文件并尝试使用Dan Galpin/Tim Bray 的著名博客文章使用 Proguard 构建 Android 包的人。
This encoding problem happens with javac (thanks for the tip, JesperE). However, I was unable to create a new javac rule on my project's files, because of needed parameters that I didn't know anything about. So here is the easy answer (probably not the only answer):
这个编码问题发生在 javac (感谢提示,JesperE)。但是,我无法在我的项目文件上创建新的 javac 规则,因为我对所需的参数一无所知。所以这是一个简单的答案(可能不是唯一的答案):
- Try the usual "ant release" command.
Notice that, on the start, there is an output talking about some imported ANT rules. Right on the start you will see some [setup] rules. Look for this one:
[setup] Importing rules file: tools\ant\ant_rules_r3.xml
Find that file and open it. Search for "javac encoding". You will see that is set to "ascii". Change to "UTF-8".
- Do the "ant release" again and it will be fine.
- 试试通常的“ant release”命令。
请注意,在开始时,有一个输出谈论一些导入的 ANT 规则。一开始你会看到一些[设置]规则。寻找这个:
[设置] 导入规则文件:tools\ant\ant_rules_r3.xml
找到那个文件并打开它。搜索“javac 编码”。你会看到它被设置为“ascii”。更改为“UTF-8”。
- 再做一次“蚂蚁释放”就可以了。
That's how I did here. I'm sure there is a way to override this on a per-project basis. But it kept giving me errors on mandatory parameters, as I said. So for me at least it was that much easier doing this way. Besides, I only work with UTF-8 anyway.
我在这里就是这样做的。我确信有一种方法可以在每个项目的基础上覆盖它。但正如我所说,它一直在强制参数上给我错误。所以至少对我来说,这样做要容易得多。此外,无论如何我只使用 UTF-8。
回答by JesperE
Are you specifying the file encoding to the compiler correctly? The java compiler will otherwise default to use the platform's default encoding, which on Windows (for example) is not UTF-8. The encoding is specified by using the -encoding flag to javac.
您是否正确地为编译器指定了文件编码?否则,java 编译器将默认使用平台的默认编码,在 Windows 上(例如)不是 UTF-8。编码是通过对 javac 使用 -encoding 标志来指定的。
javac -encoding utf8 ...
And in ant build.xml:
在 ant build.xml 中:
<javac ... encoding="UTF-8" ... />
回答by Alexander Kosenkov
Just specify encoding in your build.xml
script:
只需在build.xml
脚本中指定编码:
<javac encoding="UTF-8" ... />
But for code re-use it's better to extract value into project-specific configuration file:
但是对于代码重用,最好将值提取到特定于项目的配置文件中:
<property file="build.properties" />
<property name="java.encoding" value="ascii" />
<javac encoding="${java.encoding}"
source="${java.source}" ....>
<src path="${source.dir}" />
<classpath>
<fileset ... />
</classpath>
</javac>
And then just add java.encoding=UTF-8
to build.propertiesto override encoding on per-project basis.
然后只需添加java.encoding=UTF-8
到build.properties以覆盖每个项目的编码。
回答by Gangnus
You should set the same page in many places:
您应该在许多地方设置相同的页面:
- in Ant Script
javac encoding="UTF-8" ... /
-Dfile.encoding=UTF-8
in parameters of Ant callingIn file-properties-resource-encoding - again choose the same page. You can also change it for all files in the container by the IDE (preferences for Eclipse). Then make all files to inherit it from the container.
If some files were created in wrong encoding, rework their content through PsPad or maybe, another text editor (copy-paste, change file encoding, back copy-paste). These files will be mentioned in
unmappable character
errors.
- 在 Ant 脚本中
javac encoding="UTF-8" ... /
-Dfile.encoding=UTF-8
在 Ant 调用的参数中在文件属性资源编码中 - 再次选择相同的页面。您还可以通过 IDE(Eclipse 的首选项)为容器中的所有文件更改它。然后使所有文件从容器继承它。
如果某些文件是以错误的编码创建的,请通过 PsPad 或另一个文本编辑器(复制粘贴、更改文件编码、返回复制粘贴)重新编辑它们的内容。这些文件将在
unmappable character
错误中提及。
After setting all these four problems you will have NO unmappable character
lines.
设置所有这四个问题后,您将没有unmappable character
线路。
回答by Ravi Kumar
below is the usage of -Dfile.encoding=UTF-8 in ant build.xml file:
下面是ant build.xml文件中-Dfile.encoding=UTF-8的用法:
<java classname="${main-class}" fork="true">
<jvmarg value="-Dfile.encoding=UTF-8"/>
This will solve the problem. Working for all UTF 8 char's.
这将解决问题。适用于所有 UTF 8 字符。