Native Demo

The Native Demo illustrates using native methods through JNI (Java Native Interface) in an OSGi framework. To learn more on using native methods in bundles, refer to the Writing Bundles document.

Demo Components

The Native Demo is a bundle, which holds a native library and uses its only method. The demo's JAR is nativedemo.jar, located in the demo/bundles directory.

The Native Demo components, including Java and C source files, DLL and SO libraries as well as building scripts, are placed in the demo/framework/native directory.

Basically, the Native Demo consists of one class - demo.nat.NativeDemoActivator, which is the bundle activator calling a native method.

Starting the Demo

Prerequisites

OS and Processor Aliases

It is possible that the target virtual machine returns OS and processor aliases, different from their commercial names. As usually bundles use commercial names in their Bundle-NativeCode headers, it may happen that the framework won't locate the native library for the given platform due to mismatch between the OS/processor alias, returned by the JVM, and the one, defined in the clause of the manifest header. To avoid such faults, you can use the os.aliases and processor.aliases system properties to introduce the OS/processor commercial name to the framework. The framework will use the system properties for additional check of bundle native code dependencies.

Native Library Storage

If you are using a JDK 1.1.x-compliant JVM, you should set the mbs.storage.native system property to jdk11. In addition, you should include in the environment's PATH or LD_LIBRARY_PATH variable the directory, where the framework will place extracted native libraries, as indicated by the mbs.storage.nativedir system property (bin/vms/<vm_name>/storage/native by default).

Using an Install Script

The Native Demo has an install script for easier and faster installation. To get more details about starting the demo through this install script, refer to General Rules for Demos.

Activating All Necessary Components Manually

  1. Start the mBS framework.
  2. Install and start the Native Demo Bundle.
    For example using the framework console:
    fw.i -s ../../../demo/bundles/nativedemo.jar

Inside the Demo

Note: To be able to rebuild the demo, you will have to modify the mk script:

Bundle Activator

The Native Demo bundle consists only of one class - the bundle activator, called demo.nat.NativeDemoActivator. The native library is loaded in a static initializer, and the native method is invoked in the bundle activator's start method.

Native Method Implementation

The demo's native method, called multiply, multiplies 2 integers and returns the result. It is implemented in the demo_nat_NativeDemoActivator.c file.

Manifest

To specify the required native code library to the framework, the Native Demo Bundle's manifest contains the Bundle-NativeCode header with the appropriate information.

Bundle-NativeCode: demonative.dll;
osname=WindowsXP;
osname=Windows2000;
osname=Windows95;
osname=Windows98;
osname=WindowsNT; processor=x86;
processor=i386;
processor=i686;
processor=i586 ,
libdemonative.so;
osname=Linux;
processor=x86; processor=i386;
processor=i586;
processor=i686
Listing 1: Native Demo Bundle-NativeCode dependency.

Demos