0

I'm using pcap4j in my OSGI project. The devices the application is running on is running openembedded. When I'm running my code in a standalone application (non OSGI) on a device, it works without problem. When running inside an OSGI it doesn't. I embedded the pcap4j dependencies as they are not bundles. The manifest contains the import of com.sun.jna and the bundle jna 4.2.0 is available (and used by another bundle). I also tried by calling System.load and System.loadLibrary...

Maarten
  • 41
  • 5
  • Need more information. What does the `Bundle-NativeCode` header look like? What is the layout of your bundle? What parameters did you pass to `System.loadLibrary`? Etc. – Neil Bartlett Feb 05 '18 at 15:54
  • Also need the error message! – Neil Bartlett Feb 05 '18 at 15:56
  • Sorry, was on sick leave. It is loading the native library. The problem is that it always returns 0 on calling NativeMappings.pcap_findalldevs(alldevsPP, errbuf); And nicely returning the list of devices when on standalone app. There are no errors in the log. – Maarten Feb 08 '18 at 10:28
  • I have my bundle, that embeds the pcap4j jar. groupId=org.pcap4j;scope=compile|runtime;inline=truefalse – Maarten Feb 08 '18 at 10:34
  • I don't think you should embed the dependency (why does everybody always want to embed dependencies?!?). That way you hide the native library away in a nested JAR file. No wonder OSGi has difficulty finding it. – Neil Bartlett Feb 08 '18 at 16:15
  • I embedded it because it is not an OSGI bundle, and I don’t want to expose this functionality. The native libs are installed in the OS (OpenEmbedded) and is accessing it via JNA. – Maarten Feb 10 '18 at 12:37
  • I found the it. I was using JNA version 4.2.0. That one has no native libraries for Linux-arm. Upgrading to version 4.2.1 fixed it. – Maarten Feb 16 '18 at 18:20

1 Answers1

0

I've got the same problem too and I made an osgi bundle for pcap4j and its dependents. Here's the jar files in the bundle:

jna-4.2.1.jar, 
pcap4j-core-1.7.3.jar, 
pcap4j-packetfactory-static-1.7.3.jar,
slf4j-api-1.7.12.jar, 
slf4j-nop-1.6.2.jar

The entire MANIFEST.MF file:

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Pcap4j
Bundle-SymbolicName: pcap4j
Bundle-Version: 1.0.0
Bundle-ClassPath: jna-4.2.1.jar, pcap4j-core-1.7.3.jar, pcap4j-packetfactory-static-1.7.3.jar, slf4j-api-1.7.12.jar, slf4j-nop-1.6.2.jar
Export-Package: com.sun.jna,com.sun.jna.ptr,com.sun.jna.win32,org.pcap4j,org.pcap4j.core,org.pcap4j.packet,org.pcap4j.packet.constant,org.pcap4j.packet.factory,org.pcap4j.packet.namednumber,org.pcap4j.util,org.slf4j,org.slf4j.helpers,org.slf4j.impl,org.slf4j.spi
Bundle-RequiredExecutionEnvironment: JavaSE-1.8

After that you can simply use these packages using Import-Package.

Import-Package: org.osgi.framework;version="1.3.0", org.pcap4j.core, org.pcap4j.packet, org.pcap4j.packet.namednumber

Also look at this issue on pcap4j repository to more information.