2012-12-06 28 views
0

好的,這裏是我的問題, 我試圖讓jnetpcap在ubuntu上工作,但我遇到了一些問題。我嘗試使用jnetpcap網站上的說明進行安裝,並且將jar添加到構建路徑中並沒有問題,但似乎無法在Eclipse上運行任何程序。我的代碼如下。我收到的錯誤消息是「無法找到任何設備,錯誤是」。除了將jar添加到構建路徑上還有其他問題嗎?除了將jar添加到構建路徑之外,還有其他事情我應該完成嗎?ubuntu上的Jnetpcap

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

}

回答

0

該錯誤可能是用罐子或.so文件做。我相信你必須在eclipse中手動加載jar,並將共享目標文件放入項目中。希望這可以幫助。

0

請先以root身份運行eclipse來運行你的代碼。我在使用這個庫時遇到了同樣的問題,當我以root身份運行eclipse時,它得到了修復。你可以打開一個終端並輸入以下命令來運行eclipse:

     sudo /your/eclipse/directory/eclipse