为什么需要在Java源文件中转义unicode? [英] Why do I need to escape unicode in java source files?

查看:177
本文介绍了为什么需要在Java源文件中转义unicode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,我不是在问为什么,而是在问为什么.而且我不知道这是RCP特定的问题还是Java固有的问题.

Please note that I'm not asking how but why. And I don't know if it's a RCP specific problem or if it's something inherent to java.

我的Java源文件以UTF-8编码.

My java source files are encoded in UTF-8.

如果我这样定义文字字符串:

If I define my literal strings like this :

    new Language("fr", "Français"),
    new Language("zh", "中文")

通过在Eclipse中将其作为Eclipse应用程序启动时,它可以按我期望的方式在应用程序中使用该字符串:

It works as I expect when I use the string in the application by launching it from Eclipse as an Eclipse application :

但是当我启动由"Eclipse产品导出向导"构建的.exe时,如果失败:

But if fails when I launch the .exe built by the "Eclipse Product Export Wizard" :

我使用的解决方案是像这样转义字符:

The solution I use is to escape the chars like this :

    new Language("fr", "Fran\u00e7ais"), // Français
    new Language("zh", "\u4e2d\u6587") // 中文

这样做没有问题(我所有其他字符串都在属性文件中,只有语言名称是硬编码的),但是我想理解.

There is no problem in doing this (all my other strings are in properties files, only the languages names are hardcoded) but I'd like to understand.

我认为编译器在构建字节码时必须转换Java文字字符串. .那么,为什么要对Unicode进行转义呢?在Java源文件中使用高范围Unicode字符是错误的吗?那些char在编译时到底发生了什么,与转义char的处理有何不同?问题仅与RCP缓存有关吗?

I thought the compiler had to convert the java literal strings when building the bytecode. So why is the unicode escaping necessary ? Is it wrong to use use high range unicode chars in java source files ? What happens exactly to those chars at compilation and in what it is different from the handling of escaped chars ? Is the problem just related to RCP cache ?

推荐答案

似乎Eclipse产品导出向导未将文件解释为UTF-8.也许您需要使用设置为UTF-8(eclipse.ini中的-Dfile.encoding=UTF8)的编码来运行Eclipse的JVM?

It appears that the Eclipse Product Export Wizard is not interpreting your files as UTF-8. Perhaps you need to run Eclipse's JVM with the encoding set to UTF-8 (-Dfile.encoding=UTF8 in eclipse.ini)?

(应OP请求复制)

这篇关于为什么需要在Java源文件中转义unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆