3

My project has two bundles, lets say Bundle A and Bundle B.

Now Bundle A needs to access a class from Bundle B.

How can I do that?

palacsint
  • 28,416
  • 10
  • 82
  • 109
baklap
  • 2,145
  • 6
  • 28
  • 41

2 Answers2

6

There are two ways:

  1. Add the dependency of bundle B to the Manifest of bundle A: Require-Bundle: bundle-id-of-bundle-A
  2. Import the package of the wanted class in the Manifest of bundle A: Import-Package: package.of.your.class

In both situations, you need to export the package that contains your class in bundle B: Export-Package: package.of.your.class

Also, here's a good intro: http://ctpjava.blogspot.com/2010/09/introduction-to-osgi.html

thobens
  • 1,729
  • 1
  • 15
  • 34
  • 3
    As @thobens states there are two ways; but Require-Bundle is bad practice, you should only use Import-Package - in OSGi the package is the unit of modularity and this is what you should depend on, see http://stackoverflow.com/questions/1865819/when-should-i-use-import-package-and-when-should-i-use-require-bundle, http://njbartlett.name/2011/02/09/uses-constraints.html and http://www.osgi.org/blog/2011/05/unbearable-lightness-of-jigsaw.html for more indepth discussions of why Require-Bundle is bad. – earcam Oct 27 '11 at 09:36
1

As @earcam wrote, it is strongly recommended to use Import-Package:.

Also, always add a version to the exported package in bundle B - this is good practice that you will appreciate later when you create the next version of bundles A and B.

palacsint
  • 28,416
  • 10
  • 82
  • 109
pooh
  • 652
  • 3
  • 2