0

好的,这是我的问题,我试图让 jnetpcap 在 ubuntu 中工作,但我遇到了一些问题。我尝试使用 jnetpcap 网站上的说明进行安装,将 jar 添加到构建路径很好,但我似乎无法在 Eclipse 上运行任何程序。我的代码如下。我收到的错误消息是“找不到任何设备,错误是”。除了将 jar 添加到构建路径之外,安装时我还应该做些什么,还是另一个问题?

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
import org.jnetpcap.packet.PcapPacket;
import org.jnetpcap.packet.PcapPacketHandler;


public class apples {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    List<PcapIf> devices = new ArrayList<PcapIf>();
    StringBuilder error = new StringBuilder();

    int x = Pcap.findAllDevs(devices, error);

    if(x==Pcap.NOT_OK||devices.isEmpty()){
        System.err.printf("Can't find any devices, error is %s", error.toString());
        return;
    }

    System.out.println("Network devices found");

    PcapIf device = devices.get(0);

    int snaplen = 64*1024;
    int flags = Pcap.MODE_PROMISCUOUS;
    int timeout = 10*1000;
    Pcap open = Pcap.openLive(device.getName(), snaplen, flags, timeout, error);

    if(open==null){
        System.err.printf("Error while opening device for capture:"+error.toString());
        return;
    }
    PcapPacketHandler<String> printSummaryHandler = new PcapPacketHandler<String>(){

        public void nextPacket(PcapPacket packet, String user){
            Date timestamp = new Date(packet.getCaptureHeader().timestampInMillis());
            int caplen = packet.getCaptureHeader().caplen();
            int len = packet.getCaptureHeader().wirelen();

            System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n", timestamp.toString(), caplen, len, user);
        }
    };

    open.loop(10, printSummaryHandler, "Message!" );

    open.close();
}

}

4

2 回答 2

0

请首先以 root 身份运行 eclipse 来运行您的代码。我曾经在这个库中遇到过同样的问题,当我以 root 身份运行 eclipse 时它得到了修复。您可以打开终端并键入以下命令来运行 eclipse:

                       sudo /your/eclipse/directory/eclipse
于 2014-06-13T11:01:36.903 回答
0

该错误可能与 jar 或 .so 文件有关。我相信您必须在eclipse中手动加载jar并将共享对象文件放入项目中。希望这可以帮助。

于 2013-08-22T10:00:01.903 回答