1

This is not a repeat of Loading a Java Charset manually. I want to do the same thing, but without using META-INF. Is it possible? Is a general solution for various service types?

In this specific case, I need to process utf-7 data, using a CharsetProvider like JUTF7. In addition to adding the jar to the classpath, this requires adding a file to META-INF/services, where the file name is java.nio.charset.spi.CharsetProvider and the contents identifies jutf7. However, it is not possible to use META-INF in this case. Is there a workaround?

Community
  • 1
  • 1
Jeff Learman
  • 2,914
  • 1
  • 22
  • 31

1 Answers1

2

No, it is not possible to register a new CharSet, other than by implementing a CharSetProvider, and it can only be registered thru META-INF/services. See the javadoc.

However, that doesn't mean that you have to provide that file for jutf7. The jutf7 jar file already has the necessary service file. As the page you linked says: Just drop the jar in your classpath, and you are ready to go.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thanks for the tip, which suggested a workaround. Unfortunately, it fails because the system class loader is not the same class loader used to load user-imported classes, in my case, and the character set provider service uses the system class loader directly. There's a solution involving reflection but it's not pretty. – Jeff Learman Aug 10 '16 at 19:16
  • 3
    That is an ancient unresolved bug: [JDK-4619777 : (cs) Charset providers are not looked up via the context class loader](http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4619777) – Andreas Aug 10 '16 at 19:50