OSGi Bundle Both Imports and Exports the Same Package | Community
Skip to main content
Level 2
July 15, 2025
Solved

OSGi Bundle Both Imports and Exports the Same Package

  • July 15, 2025
  • 1 reply
  • 361 views

In OSGi bundle, what will happen if you define a package in both Export-Package and Import-Package in the bnd configuration, and how can this lead to class loading issues?

Best answer by SantoshSai

Hi @mateuszno2,

When the same package is declared in both Export-Package and Import-Package of your OSGi bundle's bnd configuration, it creates a conflict between the bundle acting as a provider and simultaneously a consumer of that package.

Here is what happens:

  • OSGi will prefer the imported version if the same package is available from another bundle.

  • This may cause version mismatches, especially if the imported version differs from the local (exported) one.

  • It can also result in split packages, where classes from the same package are loaded by different classloaders, leading to ClassCastException, NoClassDefFoundError, or unexpected behavior.

Please refer below link for more details: https://techinnovia.com/package-import-and-export/

1 reply

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
July 15, 2025

Hi @mateuszno2,

When the same package is declared in both Export-Package and Import-Package of your OSGi bundle's bnd configuration, it creates a conflict between the bundle acting as a provider and simultaneously a consumer of that package.

Here is what happens:

  • OSGi will prefer the imported version if the same package is available from another bundle.

  • This may cause version mismatches, especially if the imported version differs from the local (exported) one.

  • It can also result in split packages, where classes from the same package are loaded by different classloaders, leading to ClassCastException, NoClassDefFoundError, or unexpected behavior.

Please refer below link for more details: https://techinnovia.com/package-import-and-export/

Santosh Sai