I implement org.osgi.service.cm.ManagedService interface to get Karaf configuration. But when I give a Chinese value to the property, it is garbled.Initially, the files in the etc folder are encoded in latin1. I have tried to set utf-8 encoding, but it has no effect. Can anyone help me?
2 Answers
In Karaf, the configurations files (ie etc/*.cfg
) are handled by the felix subproject "fileinstall".
fileinstall doesn't support yet to specified a custom character encoding for the configuration, it uses the Properties
class and Properties.load(InputStream)
, which documents:
The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes as defined in section 3.3 of The Java™ Language Specification; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.
So, you have to encode your file in ISE-8859-1 and quote every UTF character, or use an xml file to encode your configuration files.

- 10,611
- 1
- 26
- 43
-
1Thank you very much,Jérémie B.That is the point.I change the file to ISO 8859-1 character encoding. And Then convert Chinese words to Unicode format.It runs all right now.Thanks. – happy programmer Mar 01 '16 at 01:26
-
Thank you very much, @Jérémie B. You saved a lot of my time! – Alexey Yakunin Mar 11 '16 at 08:28
-
The same concept affects Apache Felix OSGi . – bitfox Dec 13 '20 at 18:49
There is a way to change cfg files encoding. Configuration for fileinstall subproject polling etc/*.cfg files is written in config.properties file. You can add
felix.fileinstall.configEncoding = UTF-8
The solution was checked in Karaf 4

- 11
- 1