OSGi 包不显示 utf-8 字符

发布于 2024-12-27 21:37:20 字数 849 浏览 2 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁把谁当真 2025-01-03 21:37:20

一些注意事项:

  • 编译 Java 源代码时,请确保您的 编译器编码与您的编辑器编码相匹配(在IDE中通常不是问题)
  • 在运行时Java字符串始终为UTF-16(类格式将文字存储为修改后的UTF-8,但开发人员不这样做必须担心这一点)
  • System.out 会将 UTF-16 字符串转码为 默认平台编码;如果编码不是 Unicode,这可能是一个有损过程
  • 如果 stdout 流使用者(控制台或任何其他应用程序)不使用相同的编码对其输入进行解码,则可能会发生字符损坏
  • 不支持用于更改 JRE 中默认平台编码的机制(包括使用-Dfile.encoding=foo)

如何更正输出取决于您尝试写入的设备。请参阅此处此处用于 cmd.exe。请参阅此处了解更多一般信息关于Java编码。

A few notes:

  • When compiling Java sources, ensure your compiler encoding matches your editor encoding (usually not a problem in an IDE)
  • At runtime Java Strings are always UTF-16 (the class format stores literals as modified UTF-8, but developers don't have to worry about this)
  • System.out will transcode the UTF-16 strings to the default platform encoding; this may be a lossy process if the encoding is not Unicode
  • If the stdout stream consumer (console or whatever other application) does not decode its input using the same encoding, character corruption may occur
  • There is no supported mechanism for changing the default platform encoding in the JRE (and that includes using -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.

云雾 2025-01-03 21:37:20

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文