1

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?

Benjamin
  • 3,217
  • 2
  • 27
  • 42

2 Answers2

4

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.

McDowell
  • 107,573
  • 31
  • 204
  • 267
3

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)

Community
  • 1
  • 1
earcam
  • 6,662
  • 4
  • 37
  • 57