concept JAR file in category osgi

appears as: JAR files, JAR file, JAR files, JAR file
OSGi in Action: Creating Modular Applications in Java

This is an excerpt from Manning's book OSGi in Action: Creating Modular Applications in Java.

The module layer isn’t related to code creation as such; rather, it’s related to the packaging of your code into bundles. You need to be aware of certain code-related issues when developing, but by and large you prepare code for the module layer by adding packaging metadata to your project’s generated JAR files. For example, suppose you want to share the following class.

Listing 1.2. Basic greeting implementation
package org.foo.hello; public class Greeting { final String m_name; public Greeting(String name) { m_name = name; } public void sayHello() { System.out.println("Hello, " + m_name + "!"); } }

During the build process, you compile the source code and put the generated class file into a JAR file. To use the OSGi module layer, you must add some metadata into your JAR file’s META-INF/MANIFEST.MF file, such as the following:

As you saw in chapter 2, a bundle is a JAR file with additional metadata. So to turn a JAR file into a bundle, you need to add metadata giving it a unique identity and describing what it imports and exports. Simple, right? For most business-domain JAR files, it is; but for others (such as third-party GUI or database libraries), you’ll need to think carefully about their design. Where is the line between what’s public and what’s private, which imports are required and which are optional, and which versions are compatible with one another?

In this section, we’ll help you come up with this metadata by taking a series of common library JAR files and turning them into working bundles. We’ll also consider some advanced bundling techniques, such as embedding dependencies inside the bundle, as well as how to manage external resources and background threads.