29

I have been working with OSGi for a while now, but I still don't understand something about private packages.

  • An exported package is a package with is visible to other bundles
  • An imported package is a package which is imported by a bundle exporting the package.
  • A private package is a package which is not visible to other bundles (I don't get this)

Aren't all bundle packages which are not exported invisible to all other packages? If so, what's the difference to private packages and packages which are not exported?

I've read OSGi in Action and "OSGi and Apache Felix 3.0 - Beginners Guide", but i was not able to find the difference.

btiernay
  • 7,873
  • 5
  • 42
  • 48
Mirco
  • 2,940
  • 5
  • 34
  • 57
  • Where exactly did you encounter the concept of "private package"? Where is it configured? It is not a feature of manifest.mf, I believe. – Marko Topolnik Dec 14 '12 at 12:18
  • 2
    Please see http://stackoverflow.com/questions/13514123/what-does-the-private-package-manifest-header-do as well – btiernay Mar 28 '14 at 22:25

2 Answers2

28

There is no difference.

Any package which is not listed in Export-Package is private.... that's all there is to it.

You may have seen another header called Private-Package. This is NOT an OSGi header and it is completely ignored by the OSGi Framework. If you see this it indicates that a bundle has been built with bnd or the Maven Bundle Plugin, which uses this header has a build-time instruction. It's therefore only relevant at build time, and has no effect whatsoever at run time.

btiernay
  • 7,873
  • 5
  • 42
  • 48
Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
21

Yes, all packages not defined in the manifest.mf entry Export-Package are private packages. You don't need to specify them seperately, it's just another term for the ease of communication.

If you have your manifest generated as for example by the maven-bundle-plugin, this term get's a little more relevant, because the maven bundle plugin will per default export all packages, except for example a package called internal (or subpackages of that). This is somehow the inverse approach since you specify the private packages and have the exported package calculated. See the maven bundle plugin for details.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
benjamin
  • 1,066
  • 11
  • 22
  • 2
    Packages that are not included in the "Export-package" are not private, they are forbidden. They can't be and won't available to other bundles. Private packages on the other hand MUST be included in the "Export-package", but are supposed to be used internally in well known bundles and its using is mostly discourage by compiler. If you use them and run OSGi in strict mode, they won't be available as if they wasn't exported and you get classNotFound exception. – Jan Krakora Dec 14 '12 at 13:55
  • 2
    This is not true for pure OSGi. [The OSGi core specification (R5)](http://www.osgi.org/Specifications/HomePage) clearly states (Section 3.7.1): _Packages that are not exported or imported are called private packages._ – benjamin Dec 14 '12 at 17:32
  • 3
    The comment by @Behnil is inaccurate. For example: "private packages must be included in export-package" is clearly wrong, if they are included in exports then they are not private! Also, OSGi has no such thing as a "strict mode". – Neil Bartlett Dec 15 '12 at 08:35
  • Ok, everything I posted is intended to concrete OSGi implementation - Eclipse Equinox. So my apologies for confusing. Private packages in equinox MUST be included in exported package, because private means here internal. What you mean by private is in Equinox forbidden as I post above. As for strict mode, there are some more information http://aniszczyk.org/2008/03/06/osgi-equinox-and-strict-mode/ – Jan Krakora Dec 15 '12 at 19:58
  • @Benhil: the concept you refer to does not use "private". As the other say, OSGi defins the term to mean (contained packages-exported packages). – Peter Kriens Dec 17 '12 at 08:16