OSGi 包不显示 utf-8 字符
在我的 Bundle 中,我试图显示 utf-8 字符,我想我的默认字符集是 Cp1250 并且发生了奇怪的行为:
public class Activator implements BundleActivator {
public void start(BundleContext context) throws Exception {
System.out.println("ąśżłóę"); // this is what should've been displayed
System.out.println("������"); // this is the utf8 above encoded to cp1250
}
public void stop(BundleContext context) throws Exception {
}
public static void main(String args[]){
System.out.println("ąśżłóę"); //utf-8
System.out.println("������"); //cp1250
}
}
当我运行 main 时输出,我得到了我所期望的:
ąśżłóę
ąśżłóę
当我从 OSGi 框架启动 Bundle 时输出,字符编码从 utf-8 到 cp1250。所以输出正好相反。
ąśżłóę
ąęźł
所以我的问题是:如何处理?我应该用 cp1250 而不是 utf-8 编写应用程序吗?或者是否可以更改 osgi 默认字符集?
In my Bundle, I'm trying to display utf-8 characters, I suppose my default charset is Cp1250 and strange behaviour happens:
public class Activator implements BundleActivator {
public void start(BundleContext context) throws Exception {
System.out.println("ąśżłóę"); // this is what should've been displayed
System.out.println("������"); // this is the utf8 above encoded to cp1250
}
public void stop(BundleContext context) throws Exception {
}
public static void main(String args[]){
System.out.println("ąśżłóę"); //utf-8
System.out.println("������"); //cp1250
}
}
Output when I run main, I get what I expected:
ąśżłóę
ąśżłóę
Output when I start a Bundle from an OSGi Framework, characters are encoded from utf-8 to cp1250. So the output is exactly opposite.
ąśżłóę
ąęźł
So my question is: how to deal with it? Should I write an application in cp1250 instead of utf-8? Or is it possible to change osgi default charset?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些注意事项:
System.out
会将 UTF-16 字符串转码为 默认平台编码;如果编码不是 Unicode,这可能是一个有损过程stdout
流使用者(控制台或任何其他应用程序)不使用相同的编码对其输入进行解码,则可能会发生字符损坏-Dfile.encoding=foo
)如何更正输出取决于您尝试写入的设备。请参阅此处和此处用于 cmd.exe。请参阅此处了解更多一般信息关于Java编码。
A few notes:
System.out
will transcode the UTF-16 strings to the default platform encoding; this may be a lossy process if the encoding is not Unicodestdout
stream consumer (console or whatever other application) does not decode its input using the same encoding, character corruption may occur-Dfile.encoding=foo
)How you would correct the output would depend on the device you're trying to write to. See here and here for cmd.exe. See here for more general info on Java encoding.
AFAIK OSGi 不会对默认字符集执行任何操作。听起来您正在从 IDE 运行测试,该测试给出了正确的结果,但是当启动 OSGi 框架时,JVM 将从操作系统默认值(Windows?)获取字符集。
使用
-Dfile.encoding=UTF-8
启动您的框架(更多信息请参阅此答案)AFAIK OSGi doesn't doing anything to the default charset. It sounds like you're running a test from the IDE which gives correct result, but when the OSGi framework is being launched the JVM is getting the charset from the OS defaults (windows?).
Start your framework with
-Dfile.encoding=UTF-8
(more info see this answer)