If you've been working with OSGi, you've certainly seen this error before: Unresolved requirement: Import-Package: sun.misc
The cause of the problem is that OSGi doesn't supply the entire JRE to its bundles - it only provides the most standardized packages. This means that non-standard packages like sun.misc
and sun.reflect
aren't available to your bundles, even though they are in common usage.
The easiest way to fix this is with an extension bundle. This is a fragment bundle which contains nothing besides a manifest that looks like this:
Export-Package: sun.misc
Fragment-Host: system.bundle; extension:=framework
Bundle-SymbolicName: com.diffplug.osgi.extension.sun.misc
Bundle-ManifestVersion: 2
Manifest-Version: 1.0
This forces the OSGi system bundle to export the sun.misc
package, thus fixing the Unresolved requirement
errors.
This project provides extension bundles for the following packages:
sun.misc
(MavenCentral)sun.reflect
(MavenCentral)com.sun.net.httpserver
(MavenCentral)
They are available on MavenCentral at coordinates following this convention:
com.diffplug.osgi:com.diffplug.osgi.extension.<package>:0.0.0
If you need an extension bundle for a different package, file an issue and we'd be happy to publish it for you. Or you can make your own bundle using the following recipe:
- set the
PKG
variable ingradle.properties
- run
gradlew build
- your extension bundle will be in
build/libs
For a more detailed description of what's going on, the Spring blog has a detailed article. We're pretty convinced that the extension bundles provided above are the fastest and clearest ways to address this issue, but here's a summary of the alternatives.
Another way to accomplish this same effect is to set org.osgi.framework.system.packages=sun.misc,sun.reflect
. This will fix import errors around sun.misc
and sun.reflect
, but now you will have new errors about javax.swing
and every other standard java package. By setting this property, you are overriding all of the framework default packages, so you're likely to cause new problems.
If you happen to be using OSGi 4.2 or later, you can set org.osgi.framework.system.packages.extra=sun.misc,sun.reflect
, and it will simply add those two packages to the system default packages. This has the exact same effect as including the extension bundles for sun.misc
and sun.reflect
.
A slightly different choice is to set org.osgi.framework.bootdelegation=sun.misc.*,sun.reflect.*
. This will fix the errors, but will do so by automatically providing these packages to all bundles, whether they imported them or not. It's a subtle difference, and it usually doesn't matter, but you can try it if you want.
- Thanks to Spring for their excellent blog post on this subject.
- Maintained by DiffPlug.