1

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

Raj
  • 496
  • 7
  • 27

1 Answers1

1

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.

Frank Lee
  • 2,728
  • 19
  • 30
  • Hi @Frank Lee Thanks for your reply. I am trying it in both ways. one is, I am expecting to use system .so library. I am not sure If I need to add that so library with my bundle or Can I use it directly from platform. Second, I have copied .so library to the native directory of my bundle. . but when I build the bundle I am not getting .so library in generated jar. I change `native/libexample.so` in above pom file. please guide me how can I ship .so to the generated jar and how to use that at runtime. – Raj Sep 09 '17 at 01:33
  • 1
    You can't just list the path to the library file in your bundle. You also have to add the metadata about the platform that the library relates to, as shown in Frank Lee's answer. I.e. you have to specify at least the OS name and processor. For full details see section 3.10 of the OSGi Core Release 6.0 specification. – Neil Bartlett Sep 11 '17 at 07:58
  • Hi Neil thanks for you comment. I have added `native/libexample.so;osname=linux;processor=arm` to my pom file.I created native directory inside project root directory and added .so library to it. now when I build it, it gives me this error `Native library not found in JAR`. can u plz tell me what I am missing here? – Raj Sep 11 '17 at 12:49
  • I have updated the question with additional resources. plz have a look now. – Raj Sep 11 '17 at 14:20
  • Looking at your screenshot, the path to the bundle is not `native/libexample.so` but simply `libexample.so`. Just fix the Bundle-NativeCode header accordingly. – Neil Bartlett Sep 11 '17 at 17:34
  • Hi Neil, I have added libexample.so at the root of the project. still I am facing the same problem. – Raj Sep 12 '17 at 07:09
  • Just to be sure, you are running this from an ARM based machine? Does System.getProperty("os.arch"); return that? – Frank Lee Sep 12 '17 at 09:41
  • @FrankLee Please have a look at https://stackoverflow.com/questions/46236261/unsatisfiedlinkerror-while-calling-native-method-from-osgi-bundle – Raj Sep 15 '17 at 09:33