M.Stork
M.Stork

Reputation: 21

osgi unresolved requirement: osgi.native

I am trying to make a call from a bundle to a native library using JNA. The code itself works fine. As a container I am using karaf, which I cannot change. The bundle sits in the state "Installed" and when trying to start it I get the following error.

Error executing command: Error executing command on bundles:
        Error starting bundle 87: Could not resolve module: de.db.fkfmip.preparation.fkfmip-preparation-v2-gpio-nsb [87]
  Unresolved requirement: Require-Capability: osgi.native; native.paths.0:List<String>="libf403.so"; native.paths.2:List<String>="libf403.so"; native.paths.1:List<String>="libf403.so"; native.paths.4:List<String>="libf403.so"; native.paths.3:List<String>="libf403.so"; native.paths.5:List<String>="libf403.so"; filter:="(|(&(osgi.native.osname~=Linux)(osgi.native.processor~=ARM))(&(osgi.native.osname~=Linux)(osgi.native.processor~=arm_le))(&(osgi.native.osname~=Linux)(osgi.native.processor~=arm_be))(&(osgi.native.osname~=Linux)(osgi.native.processor~=x86-64))(&(osgi.native.osname~=Linux)(osgi.native.processor~=x86_64))(&(osgi.native.osname~=Linux)(osgi.native.processor~=arm)))"

Here is a screenshot of my karaf console: Karaf Console

I have made sure that I added the Bundle-NativeCode tag in my osgi.bnd

Bundle-NativeCode: \
    libf403.so;osname=Linux;processor=ARM,\
    libf403.so;osname=Linux;processor=arm_le,\
    libf403.so;osname=Linux;processor=arm_be,\
    libf403.so;osname=Linux;processor=x86-64,\
    libf403.so;osname=Linux;processor=x86_64,\
    libf403.so;osname=Linux;processor=arm

It matches my system which is openSUSE 42.3.

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit

I am not sure what I am missing. Do I need to install something extra for karaf to work with osgi.native?

Upvotes: 1

Views: 1545

Answers (1)

M.Stork
M.Stork

Reputation: 21

I have encountered many Problems using OSGi and JNA. Maybe this will help.

My first mistake was not to get all dependencies right at the beginning. The following dependencies are needed. I got weird errors when I left the second one out.

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>4.5.1</version>
</dependency>
<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna-platform</artifactId>
    <version>4.5.0</version>
</dependency>

If you are using your own native library as I did, you need to make sure you have compiled it with 32-bit or 64-bit compiler depending on the system it will be running on.

Using JNA in your code is quite straight forward. You need to make an interface matching the methods of your native library and then load the library as shown below.

/** Shared Library libf403.so */
private F403 f403;

    /** JNA interface for libf403.so */
private interface F403 extends Library {
    int BoardConfig();
    int SetOutput(byte channel, byte status);
    int GetGroupInputs();
}

f403 = (F403) Native.loadLibrary("f403", F403.class);
f403.BoardConfig();

If you have the native library in a specific location you can also specify a path. For example:

f403 = (F403) Native.loadLibrary("/usr/lib/libf403.so", F403.class);

Now onto OSGi. When you have your own library and want it to be inside the jar you can do the following. Place the native library under your source folder /scr/main/resources and in a folder you can name yourself. In my example it is linux-x86.

The bundle description then needs a Bundle-NativeCode tag. Under that tag you can define a whole list of libraries, OSs and processors. I know that my hardware and OS will never change. So that is fine for me.

Export-Package:
Import-Package: \
    com.sun.jna*;resolution:=optional, \
    *
Bundle-NativeCode: \
    linux-x86/libf403.so;osname=Linux;processor=x86 

-dsannotations: *
-dsannotations-options: norequirements
-metatypeannotations: *

Be aware that you do not need the Bundle-NativeCode tag if you are not having the library in your resources or vice versa. You will only get compile errors.

Upvotes: 1

Related Questions