0

Okay so here's my problem, I'm trying to get jnetpcap working in ubuntu but I'm having a few issues. I tried using the instructions on the jnetpcap website to install, and it was fine for the adding the jar to the build path but I can't seem to run any programs on Eclipse. My code is below. The error message I receive is "Can't find any devices, error is". Is there something else I should have done when installing besides adding the jar to the build path or is it another problem?

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

}

Shane
  • 461
  • 2
  • 8
  • 23

2 Answers2

0

The error is probably to do with the jar or the .so file. You have to manually load the jar in eclipse I believe and put the shared object file in the project. Hope this helps.

mccormickt12
  • 575
  • 1
  • 7
  • 14
0

Kindly run your code by first running eclipse as root. I had the same problem once with this library and it got fixed when i ran eclipse as root. You can open a terminal and type the following command to run eclipse:

                       sudo /your/eclipse/directory/eclipse
4aRk Kn1gh7
  • 4,259
  • 1
  • 30
  • 41