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.
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.
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.
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).
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.
fw.i -s ../../../demo/bundles/nativedemo.jarMBS_HOME variable must be changed so that it points to
a valid mBS installation directory, as server.jar needs
to be in the classpath to compile the demo.JAVA_HOME
variable.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.
The demo's native method, called multiply, multiplies 2 integers
and returns the result. It is implemented in the demo_nat_NativeDemoActivator.c
file.
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; |