Raj
Raj

Reputation: 506

How to include and use .SO library files in OSGI Bundles

I am new to osgi bundle development. I am developing osgi bundle to run on the ubuntu machine. I want to access shared libraries (.so file) from osgi bundle. I am generating osgi bundle jar using maven plugin.

I have added .so in my code like below image

shows code structure

Here is my pom file which generates osgi bundle jar.

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Embed-StripVersion>true</Embed-StripVersion>
                    <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
                    <Bundle-Name>${pom.artifactId}</Bundle-Name>
                    <Bundle-Version>${pom.version}</Bundle-Version>
                    <Import-Package>org.osgi.framework,
                        javax.net.ssl, javax.net, 
                        com.google.gson, 
                        com.google.gson.reflect,
                        org.osgi.service.blueprint.container
                    </Import-Package>
                    <Bundle-Activator>com.example.ModuleActivator</Bundle-Activator>
                    <Embed-Dependency></Embed-Dependency>
                    <Bundle-ClassPath>libexample.so, .</Bundle-ClassPath>
                    <Bundle-NativeCode>libexample.so</Bundle-NativeCode>
                    <Export-Package />
                    <Require-Capability />
                    <Embedded-Artifacts />
                </instructions>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <filtering>false</filtering>
            <directory>${basedir}/native</directory>
            <includes>
                <include>libexample.so</include>
            </includes>
    </resource>
    <resources>

below code is used to access the method defined in so library

System.loadLibrary("example"); test_app.helloWorld();

When I run simple java code without osgi, then I can call the .so library functions successfully. I can get the output fromtest_app.helloWorld();.

When I call System.loadLibrary("example"); from osgi bundle from bundle activator I am not getting any Exception. but when I call the library functions I am getting the exception java.lang.UnsatisfiedLinkError.

This is the generated manifest file from maven plugin

`

Manifest-Version: 1.0
Bnd-LastModified: 1505136984891
Build-Jdk: 1.8.0_121
Built-By: Raj Sharma
Bundle-Activator: com.example.ModuleActivator
Bundle-ClassPath: libexample.so, .
Bundle-ManifestVersion: 2
Bundle-Name: ExampleModule
Bundle-NativeCode: libexample.so ; osname=linux ; processor=arm
Bundle-SymbolicName: ExampleModule
Bundle-Version: 2.0.5.Release
Created-By: Apache Maven Bundle Plugin
Embed-StripVersion: true
Import-Package: org.osgi.framework;version="[1.8,2)",javax.net.ssl,javax
 .net,com.google.gson;version="[2.5,3)",com.google.gson.reflect;version=
 "[2.5,3)",org.osgi.service.blueprint.container;version="[1.0,2)"
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Tool: Bnd-3.0.0.201509101326

`

and this is the generated bundle content

jar output

Upvotes: 1

Views: 2103

Answers (1)

Frank Lee
Frank Lee

Reputation: 2748

It's fiddly to get to work, but in my experience once you have it in place it is pretty easy to work with.

I'm not too familiar with the Maven config, but are you embedding your native code? It looks like you're addressing an explicit file on the system, don't know if that works well.

So to figure it out:

    Check your bundle, does it contain the native code?
    Compare it with a 'known' native bundle like [this][1] (for example)
    Do the paths in the manifest correspond with the paths of the native files within the bundle?
    Check if the architecture matches with the OS/arch:

Bundle-NativeCode: lwjgl32.dll;jemalloc32.dll;osname=Windows; processo r=x86,lwjgl.dll;jemalloc.dll;osname=Windows; processor=x86-64,liblwjg l.so;libjemalloc.so;osname=Linux; processor=x86-64,liblwjgl.dylib;lib jemalloc.dylib;osname=MacOSX; processor=x86-64

Otherwise, please post your MANIFEST file and the layout of your bundle jar.

Upvotes: 1

Related Questions