关于JNetPcap的问题
我使用了官网上的JNetPcap实例,JNetPcap.jar和JNetPcap.dll都导入到项目中了,源码如下:
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.jnetpcap.Pcap;
import org.jnetpcap.PcapHandler;
import org.jnetpcap.PcapIf;
@SuppressWarnings("deprecation")
public class ClassicPcapExample {
public static void main(String[] args) {
List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs
StringBuilder errbuf = new StringBuilder(); // For any error msgs
/********************************************
* 取得设备列表
********************************************/
int r = Pcap.findAllDevs(alldevs, errbuf);
if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
System.err.printf("Can't read list of devices, error is %s", errbuf
.toString());
return;
}
System.out.println("Network devices found:");
int i = 0;
for (PcapIf device : alldevs) {
System.out.printf("#%d: %s [%s]\n", i++, device.getName(), device
.getDescription());
}
PcapIf device = alldevs.get(2); // We know we have atleast 1 device
System.out.printf("\nChoosing '%s' on your behalf:\n", device
.getDescription());
/***************************************
* 打开选中的设备
***************************************/
int snaplen = 64 * 1024; // Capture all packets, no trucation
int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
int timeout = 10 * 1000; // 10 seconds in millis
Pcap pcap = Pcap
.openLive(device.getName(), snaplen, flags, timeout, errbuf);
if (pcap == null) {
System.err.printf("Error while opening device for capture: "
+ errbuf.toString());
return;
}
/**********************************************************************
* Third we create a packet hander which will be dispatched to from the
* libpcap loop.
**********************************************************************/
PcapHandler<String> printSummaryHandler = new PcapHandler<String>() {
public void nextPacket(String user, long seconds, int useconds,
int caplen, int len, ByteBuffer buffer) {
Date timestamp = new Date(seconds * 1000 + useconds/1000); // In millis
System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n",
timestamp.toString(), // timestamp to 1 ms accuracy
caplen, // Length actually captured
len, // Original length of the packet
user // User supplied object
);
}
};
/************************************************************
* Fourth we enter the loop and tell it to capture 10 packets
************************************************************/
pcap.loop(10, printSummaryHandler, "jNetPcap rocks!");
/*
* Last thing to do is close the pcap handle
*/
pcap.close();
}
}
但是运行后出现如下错误:
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 cn.cauc.edu.gy.ClassicPcapExample.main(ClassicPcapExample.java:26)
请问大大们,如何解决??
--------------------编程问答-------------------- dll文件未加载吧? --------------------编程问答-------------------- 那应该如何解决?? --------------------编程问答--------------------
那应该如何解决? --------------------编程问答-------------------- 好像适应该放到一个叫做ext(jdk或者jre的 或 System32目录下)的文件夹中吧,我一钱使用jpcap的时候好像这样弄过,具体情况记不清楚了 --------------------编程问答--------------------
不是这个问题,问题已经解决了,不过还是要感谢你的回答。 --------------------编程问答-------------------- 怎么解决啊?? --------------------编程问答-------------------- 同问。。。。。。怎么处理的?
补充:Java , Java SE