5

I am using IntelliJ to run a sample java-jnetpcap application. I have 64 bit JDK in the class path and included the following dependency

<dependency>
  <groupId>jnetpcap</groupId>
  <artifactId>jnetpcap</artifactId>
  <version>1.4.r1425-1f</version>
</dependency>

I am running the below sample.java class

public class PcapReaderDemo
{

private static final String filePath= "/src/main/resources/TAPcapture.pcap";

public static void main(String [] arguments){

final StringBuilder errbuf = new StringBuilder();
Pcap pcap = Pcap.openOffline(filePath,errbuf);
if (pcap == null) {
  System.err.printf("Error while opening device for capture: "
    + errbuf.toString());
  return;
}
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {
  public void nextPacket(PcapPacket packet, String user) {
    System.out.printf("Received at %s caplen=%-4d len=%-4d %s\n",
      new Date(packet.getCaptureHeader().timestampInMillis()),
      packet.getCaptureHeader().caplen(), // Length actually captured
      packet.getCaptureHeader().wirelen(), // Original length
      user // User supplied object
    );
  }
};

System.out.println("Cleared");
}
}

It is throwing the below exception:

 PcapReaderDemo
 Exception in thread "main" java.lang.UnsatisfiedLinkError: com.slytechs.library.NativeLibrary.dlopen(Ljava/lang/String;)J
at com.slytechs.library.NativeLibrary.dlopen(Native Method)
at com.slytechs.library.NativeLibrary.<init>(Unknown Source)
at com.slytechs.library.JNILibrary.<init>(Unknown Source)
at com.slytechs.library.JNILibrary.loadLibrary(Unknown Source)
at com.slytechs.library.JNILibrary.register(Unknown Source)
at com.slytechs.library.JNILibrary.register(Unknown Source)
at com.slytechs.library.JNILibrary.register(Unknown Source)
at org.jnetpcap.Pcap.<clinit>(Unknown Source)
at com.demo.myapexapp.PcapReaderDemo.main(PcapReaderDemo.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Please suggest your inputs on where it is going wrong.

thevillageguy
  • 123
  • 2
  • 9
  • Websearches for `com.slytechs.library.NativeLibrary.dlopen` bring some results. Most of them suggest that you have to copy the `.so` file manually to some directory (native libraries are usually not handled via maven - that's a pain in the a... unfortunately :-/). Did you already try some of these approaches, and copy the native library to a directory where it can be found? – Marco13 Aug 20 '16 at 00:26
  • Could you paste one such url which clearly explains it? I have already tried a few approaches though but not copied .so file. – thevillageguy Aug 22 '16 at 18:04
  • The process for IntelliJ should be similar to that on http://jnetpcap.com/?q=eclipse (or for NetBeans). I'm not familiar with IntelliJ, but ... you could just try copying the native library (`.so` file on linux, `.dll` file on windows, `.dylib` file on Mac) into the main directory of your project. What have you tried so far? – Marco13 Aug 22 '16 at 19:54
  • Facing the same problem in eclipse even after following the steps mentioned in http://jnetpcap.com/?q=eclipse – Mohit Arvind khakharia Dec 14 '17 at 01:26

5 Answers5

4

I solved the same issue this way:

  1. using Ubuntu 16.04

  2. installing jre-1.8.0_181 manually:

  3. Download, extract und copy jnetpcap files to lib directory

  4. Run your program

NormanSp
  • 41
  • 2
3

I ran into this exception as well, and discovered that I had forgotten an installation step from RELEASE_NOTES.txt.

The library will fail to find the binaries unless they're placed in an OS default location, or Java is given some way to find them. For me, following the directions made this error go away.

It's hard to summarize it better than the source material, so I'll paste it here directly:

2) Setup native jnetpcap dynamically loadable library. This varies between
 operating systems.

 * On Win32 systems do only one of the following

   - copy the jnetpcap.dll library file, found at root of jnetpcap's
     installation directory to one of the window's system folders. This
     could be \windows or \windows\system32 directory.

   - add the jNetPcap's installation directory to system PATH variable. This
     is the same variable used access executables and scripts.

   - Tell Java VM at startup exactly where to find jnetpcap.dll by setting
     a java system property 'java.library.path' such as:
       c:\> java -Djava.library.path=%JNETPCAP_HOME%

   - You can change working directory into the root of jnetpcap's 
     installation directory.

 * On unix based systems, use one of the following
   - add /usr/lib directory to LD_LIBRARY_PATH variable as java JRE does not
     look in this directory by default

   - Tell Java VM at startup exactly where to find jnetpcap.dll by setting
     a java system property 'java.library.path' such as:
       shell > java -Djava.library.path=$JNETPCAP_HOME

   - You can change working directory into the root of jnetpcap's 
     installation directory.

 * For further trouble shooting information, please see the following link:
   (http://jnetpcap.wiki.sourceforge.net/Troubleshooting+native+library)
John Walthour
  • 786
  • 2
  • 9
  • 23
2

The solution that works for me was to see what was missing for libjnetpcap with ldd :

$ ldd libjnetpcap.so<br>
        linux-vdso.so.1 =>  (0x00007ffe42706000)<br>
        libstdc++.so.6 (0x00007f12ef2ad000)<br>
        libpcap.so.0.9.4 => **not found**<br>
        libc.so.6 => /lib64/libc.so.6 (0x00007f12eeefe000)<br>
        libm.so.6 => /lib64/libm.so.6 (0x00007f12eec7a000)<br>
        /lib64/ld-linux-x86-64.so.2 (0x00007f12ef861000)<br>
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f12eea63000)<br>

My version of libjnetpcap.so was searching for a libpcap.so.0.9.4. So I just made a quick link and check with ldd :

$ ln -s /usr/lib64/libpcap.so /usr/lib64/libpcap.so.0.9.4<br>
$ ldd libjnetpcap.so<br>
        linux-vdso.so.1 =>  (0x00007ffdaadee000)<br>
        libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007eff8f974000)<br>
        libpcap.so.0.9.4 => /usr/lib64/libpcap.so.0.9.4 (0x00007eff8f734000)<br>
        libc.so.6 => /lib64/libc.so.6 (0x00007eff8f39f000)<br>
        libm.so.6 => /lib64/libm.so.6 (0x00007eff8f11b000)<br>
        /lib64/ld-linux-x86-64.so.2 (0x00007eff8ff42000)<br>
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007eff8ef05000)<br>

Then all was good for me.

Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
Zaitchev
  • 41
  • 3
1

In Ubuntu, I also needed this library:

sudo apt install libpcap-dev

I also cp the files as described by @NormanSp and @user977860

Mihai.Mehe
  • 448
  • 8
  • 13
0

1st Place your libjnetpcap.so inside /lib64

2nd Make sure your java version is 1.8.0_181 or below.

user987760
  • 1,061
  • 3
  • 12
  • 26