jnetpcap - java.lang.UnsatisfiedLinkError:com.slytechs.library.NativeLibrary.dlopen(Ljava / lang / String;)J

时间:2016-08-19 23:29:45

标签: java jnetpcap

我正在使用IntelliJ来运行示例java-jnetpcap应用程序。我在类路径中有64位JDK,并包含以下依赖项

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

我正在运行下面的sample.java类

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");
}
}

抛出以下异常:

 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)

请建议您输入错误的地方。

5 个答案:

答案 0 :(得分:2)

我通过这种方式解决了相同的问题:

  1. 使用Ubuntu 16.04

  2. 手动安装jre-1.8.0_181:

  3. 下载jnetpcap文件并将其提取并复制到lib目录

  4. 运行程序

答案 1 :(得分:1)

我也遇到了这个异常,发现我忘记了RELEASE_NOTES.txt的安装步骤。

除非将它们放在操作系统的默认位置,否则库将无法找到二进制文件,或者Java会以某种方式找到它们。对我来说,遵循指示,这个错误消失了。

很难比源材料更好地总结它,所以我将它直接粘贴在这里:

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)

答案 2 :(得分:1)

在Ubuntu中,我还需要该库:

sudo apt install libpcap-dev

我还按照@NormanSp和@ user977860的说明对文件进行cp

答案 3 :(得分:0)

首先将 libjnetpcap.so 放入 / lib64

2nd确保您的 java 版本是 1.8.0_181 以下

答案 4 :(得分:0)

最适合我的解决方案是查看带有ldd的libjnetpcap缺少的内容:

$ 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>

我的libjnetpcap.so版本正在搜索libpcap.so.0.9.4. 所以我只是做了一个快速链接,并检查了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>

那一切对我都有好处。