1

I have created one OSGi plugin (Bundle) using eclipse File-->New-->Other-->Plug-in Project called plugin 1. I want to use a native .so library in this plugin. I have added libtest_app_wrap1.so at the root of my plugin project.

my project structure looks like below

plugin project structure

And here is the manifest file

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: JniTest
Bundle-SymbolicName: JniTest
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: jnitest.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Import-Package: org.osgi.framework;version="1.3.0"
Bundle-NativeCode: libtest_app_wrap1.so; osname=linux; processor=amd64

And this is the code of my Activator class

    package jnitest;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

    public Activator() {
        System.loadLibrary("test_app_wrap1");
        System.out.println("Library Loaded Successfully.......");
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        try {
            test_app.foo();

        } catch (Throwable tr) {
            tr.printStackTrace();

        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        System.out.println("Goodbye World!!");
    }

}

I create jar file from plugin project using eclipse export feature. output jar file contains .so library.

I don't get any exception or error when I call the System.loadLibrary("test_app_wrap1");, but when I call the method test_app.foo(); it gives me UnsatisfiedLinkError. test_app.foo() is a jni method which is defined in .so.

I am not getting any clue to resolve this error. Please help to get it resolve.

[Edit: Output bundle content]

Here is the content of the output jar

jar output

Raj
  • 496
  • 7
  • 27
  • Could you try `Bundle-NativeCode: /libtest_app_wrap1.so; osname=Linux; processor=x86-64` (see [OSGi Reference](https://www.osgi.org/developer/specifications/reference/))? – howlger Sep 15 '17 at 09:53
  • @howlger I have tried with `Bundle-NativeCode: /libtest_app_wrap1.so; osname=Linux; processor=x86-64` but not helped. Still same exception. I am trying on ubuntu machine. – Raj Sep 15 '17 at 10:03
  • In my understanding, with `Bundle-NativeCode: ...` OSGi loads the native code instead of `System.loadLibrary(...);` in your Java code. Could you test it without `System.loadLibrary(...);` and without an bundle activator (maybe `test_app.foo();` can only be called after activator's `start()` has been finished). – howlger Sep 15 '17 at 10:35
  • @howlger Thanks for you instant replies. yes looks like OSGi `Bundle-NativeCode` loads the library. I have tried with removing `System.loadLibrary("test_app_wrap1")` but exception is the same. – Raj Sep 15 '17 at 10:47
  • Please show the content of your actual built bundle. The path to the .so file may be incorrect. Do not begin the path with a '/' character. – Neil Bartlett Sep 15 '17 at 11:09
  • @NeilBartlett Can native code called already in or only after `BundleActivator.start()`? – howlger Sep 15 '17 at 11:21
  • @NeilBartlett Thanks for your attention. I have updated the question and removed the character `/` . – Raj Sep 15 '17 at 11:22
  • System.loadLibrary is normally called in the static initializer of the class with the native methods. So in this case you would put that in the test_app class. – BJ Hargrave Sep 16 '17 at 10:36

1 Answers1

-1

Add the jar to the project via: Project->Properties->Libraries->Add Jar Add the jar to the class path via plugin.xml->Runtime->ClassPath section->Add

or Use following ways to add jar

java -Djava.library.path

and verify by following:

System.getProperty("java.library.path")
  • there is no jar to add to the plugin project. I just want to use the `.so` library. Please let me know which jar I need to add. – Raj Sep 15 '17 at 09:55
  • https://stackoverflow.com/questions/36780017/unsatisfiedlinkerror-in-exported-eclipse-executable-jar-file –  Sep 15 '17 at 10:08
  • I am not getting any exception when I call `System.loadLibrary("test_app_wrap1");`. I get exception only when I try to invoke the method defined in library. – Raj Sep 15 '17 at 10:12