为什么我需要在java源文件中转义unicode ?

时间:2023-01-12 14:31:02

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.

请注意,我不是问为什么,而是问为什么。我不知道这是RCP特有的问题还是java固有的问题。

My java source files are encoded in UTF-8.

我的java源文件是用UTF-8编码的。

If I define my literal strings like this :

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

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

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

当我在应用程序中使用字符串作为Eclipse应用程序启动时,它的工作方式是:

为什么我需要在java源文件中转义unicode ?

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

但如果在启动“Eclipse产品导出向导”构建的.exe时失败:

为什么我需要在java源文件中转义unicode ?

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

我使用的解决方案是像这样避开chars:

    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.

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

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 ?

我认为编译器在构建字节码时必须转换java文字字符串。那么为什么unicode转义是必须的呢?在java源文件中使用高范围的unicode字符集是错误的吗?在编译过程中,这些chars到底发生了什么?它与对转义chars的处理有什么不同?这个问题仅仅与RCP缓存有关吗?

2 个解决方案

#1


10  

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)?

看起来Eclipse产品导出向导并没有将您的文件解释为UTF-8。也许您需要运行将编码设置为UTF-8 (-Dfile)的Eclipse JVM。编码= use UTF8 eclipse.ini)?

(Copypasta'd at OPs request)

(Copypasta会在运维请求)

#2


2  

When exporting a plug-in, it gets compiled through a process separate from the normal build process within the IDE. There is a known bug that the build process (PDE.Build) disregards the text encoding used by the IDE.

当导出一个插件时,它通过一个与IDE中的正常构建过程分离的过程来编译。有一个已知的错误,即构建过程忽略了IDE使用的文本编码。

The export can be made to work properly by specifying the text encoding in the build.properties file of your plugin

通过在构建中指定文本编码,可以使导出正常工作。插件的属性文件

javacDefaultEncoding.. =UTF-8

#1


10  

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)?

看起来Eclipse产品导出向导并没有将您的文件解释为UTF-8。也许您需要运行将编码设置为UTF-8 (-Dfile)的Eclipse JVM。编码= use UTF8 eclipse.ini)?

(Copypasta'd at OPs request)

(Copypasta会在运维请求)

#2


2  

When exporting a plug-in, it gets compiled through a process separate from the normal build process within the IDE. There is a known bug that the build process (PDE.Build) disregards the text encoding used by the IDE.

当导出一个插件时,它通过一个与IDE中的正常构建过程分离的过程来编译。有一个已知的错误,即构建过程忽略了IDE使用的文本编码。

The export can be made to work properly by specifying the text encoding in the build.properties file of your plugin

通过在构建中指定文本编码,可以使导出正常工作。插件的属性文件

javacDefaultEncoding.. =UTF-8