首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取java.lang.IllegalArgumentException的jnetpcap

获取java.lang.IllegalArgumentException的jnetpcap
EN

Stack Overflow用户
提问于 2013-10-11 17:35:58
回答 1查看 1.2K关注 0票数 1

我的控制台输出是--

代码语言:javascript
复制
Exception in thread "main" java.lang.IllegalArgumentException: size of array must be MAX_ID_COUNT size
at org.jnetpcap.packet.JScanner.loadScanners(Native Method)
at org.jnetpcap.packet.JScanner.reloadAll(JScanner.java:376)
at org.jnetpcap.packet.JScanner.<init>(JScanner.java:313)
at org.jnetpcap.packet.JScanner.<init>(JScanner.java:293)
at org.jnetpcap.packet.JPacket.getDefaultScanner(JPacket.java:621)
at org.jnetpcap.packet.JPacket.scan(JPacket.java:1094)
at org.jnetpcap.packet.JMemoryPacket.<init>(JMemoryPacket.java:387)
at org.jnetpcap.packet.JMemoryPacket.<init>(JMemoryPacket.java:440)
at ClassicPcapExample.main(ClassicPcapExample.java:128)

代码:

代码语言:javascript
复制
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
import org.jnetpcap.packet.JMemoryPacket;  
import org.jnetpcap.packet.JPacket;
import org.jnetpcap.packet.PcapPacket;
import org.jnetpcap.packet.PcapPacketHandler;
import org.jnetpcap.protocol.JProtocol; 
import org.jnetpcap.protocol.lan.Ethernet;
import org.jnetpcap.protocol.network.Ip4;
import org.jnetpcap.protocol.tcpip.Tcp;

public class ClassicPcapExample{

/**
 * Main startup method
 * 
 * @param args
 *          ignored
 */
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

    /***************************************************************************
     * First get a list of devices on this system
     **************************************************************************/
    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) {
        String description =
            (device.getDescription() != null) ? device.getDescription()
                : "No description available";
        System.out.printf("#%d: %s [%s]\n", i++, device.getName(), description);
    }

    PcapIf device = alldevs.get(0); // We know we have atleast 1 device
    System.out
        .printf("\nChoosing '%s' on your behalf:\n",
            (device.getDescription() != null) ? device.getDescription()
                : device.getName());

    /***************************************************************************
     * Second we open up the selected device
     **************************************************************************/
    int snaplen = 64 * 1024;           // Capture all packets, no truncation
    int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
    int timeout = 10 * 1000;           // 10 seconds in milliseconds
    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 handler which will receive packets from the
     * libpcap loop.
     **************************************************************************/
    PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {

        public void nextPacket(PcapPacket packet, String user) {

            System.out.printf("Received packet 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
                );
        }
    };

    /***************************************************************************
     * Fourth we enter the loop and tell it to capture 10 packets. The loop
     * method does a mapping of pcap.datalink() DLT value to JProtocol ID, which
     * is needed by JScanner. The scanner scans the packet buffer and decodes
     * the headers. The mapping is done automatically, although a variation on
     * the loop method exists that allows the programmer to specify exactly
     * which protocol ID to use as the data link type for this pcap interface.
     **************************************************************************/
    //pcap.loop(5, jpacketHandler, "jNetPcap rocks!");//GETTING AN ERROR AT THIS LINE!! 

    pcap.close();


}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-23 20:29:10

当我将1.4.r1425降级到1.4.r1425时,我开始遇到同样的问题,这是因为1.4.r1425版本的jnetpcap.dll文件,而我忘记了用1.3VersionDLL来替换这个文件。我建议您确保有正确版本的本机库文件可用(.dll for windows/..so for linux -as for linux-as/ ur环境)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19324090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档