4

我正在使用 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)

请就出错的地方提出您的意见。

4

5 回答 5

4

我以这种方式解决了同样的问题:

  1. 使用 Ubuntu 16.04

  2. 手动安装 jre-1.8.0_181:

  3. 下载、解压并复制 jnetpcap 文件到 lib 目录

  4. 运行你的程序

于 2019-08-06T09:29:06.447 回答
3

我也遇到了这个异常,发现我忘记了 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)
于 2018-01-07T14:28:01.360 回答
2

对我有用的解决方案是使用 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正在搜索 alibpcap.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>

然后一切都对我有好处。

于 2019-10-18T11:45:28.807 回答
1

在 Ubuntu 中,我还需要这个库:

sudo apt install libpcap-dev

我还 cp @NormanSp 和 @user977860 描述的文件

于 2020-04-24T00:30:21.250 回答
0

第一把你的libjnetpcap.so放在/lib64里面

第二确保您的java版本是1.8.0_181以下

于 2018-12-20T16:21:40.353 回答