0

Upon migration to JDK 9+, our OSGi container built around apache Felix started to fail installing an extension bundle. The error message is:

Caused by: org.osgi.framework.BundleException: Could not create bundle object.
    at org.apache.felix.framework.Felix.installBundle(Felix.java:3095)
    ...
Caused by: java.lang.UnsupportedOperationException: Unable to add extension bundle.
    at org.apache.felix.framework.ExtensionManager.addExtensionBundle(ExtensionManager.java:439)
    at org.apache.felix.framework.Felix.installBundle(Felix.java:3061)
    ...

Our Felix version is 5.6.10, which is currently the latest available. Relevant part of the bundle manifest:

Created-By: Apache Maven Bundle Plugin
Fragment-Host: system.bundle; extension:=framework
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=9.0))"
Tool: Bnd-3.5.0.201709291849
gjoranv
  • 4,376
  • 3
  • 21
  • 37

1 Answers1

2

Debugging revealed that the class loader used to install the extension has been moved to a named module in JDK 9. The class loader's package must therefore be opened for all unnamed modules in order to be reachable from the Felix framework.

It works after adding the following command-line option:

--add-opens=java.base/jdk.internal.loader=ALL-UNNAMED

For unit tests with maven-surefire-plugin, use:

 <configuration>
     <argLine>
         --add-opens=java.base/jdk.internal.loader=ALL-UNNAMED
     </argLine>
 </configuration>

The solution seems to be logged from Felix' ExtensionManager, but not added to the exception message, so the message was not found in my own logs:

        m_logger.log(bundle, Logger.LOG_WARNING,
            "Unable to add extension bundle - Maybe ClassLoader is not supported " +
                    "(on java9, try --add-opens=java.base/jdk.internal.loader=ALL-UNNAMED)?");
gjoranv
  • 4,376
  • 3
  • 21
  • 37
  • 1
    The Felix code appears to be hacking into the non-public appendToClassPathForInstrumentation method defined by the application class loader. That method is for use by the implementation of the java.instrument agent, it should never be used directly by code outside of the JDK. Hopefully there is a bug submitted to Felix on this. – Alan Bateman Jun 23 '18 at 15:19
  • @AlanBateman The Felix project is indeed aware of this issue and working on it. – Neil Bartlett Jun 28 '18 at 13:16
  • @NeilBartlett Is there a Jira issue that we can follow? – gjoranv Jun 28 '18 at 18:33
  • @NeilBartlett FELIX-5727 was fixed for 5.6.10, which is the version we're using. Does this mean the workaround using 'add-opens' is a permanent one? – gjoranv Jun 29 '18 at 13:47
  • 1
    @gjoranv Yes, I think this means that supporting OSGi Framework Extension bundles may always require an `add-opens` when running on Java 9+. I have always thought that Framework Extensions were a hacky part of the spec, and encourage people to find alternative solutions. – Neil Bartlett Jul 02 '18 at 10:09