How to run pcap4j on Mac OS X.

Pcap4J is a Java library for capturing, crafting and sending packets.

And, according to its README, Mac OS X is not included in supported operating system.

But, you can run Pcap4J on Mac OS X in the following way.

 

[Preparetion]

  1. Download pcap4j.jar from GitHub.
  2. Install libpcap using MacPorts (> sudo port install libpcap).
  3. Download jna-3.3.0.jar from here, and put it in the same folder with the pcap4j.jar. Don't use the latest JNA, or you'll have a java exception (java.lang.AbstractMethodError: com.sun.jna.Structure.getFieldOrder()Ljava/util/List).
  4. Download slf4j-1.7.2.zip from here and unarchive it. Then, put slf4j-api-1.7.2.jar and slf4j-nop-1.7.2.jar in the same folder with the pcap4j.jar (You don't have to put slf4j-nop-1.7.2.jar, but you can suppress the "SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"" message.).

 

[How to run]

Launch a terminal, and run java command like as follows.

> sudo java -cp pcap4j.jar:jna-3.3.0.jar:slf4j-api-1.7.2.jar -Dorg.pcap4j.core.pcapLibName=libpcap.dylib -Dorg.pcap4j.sample.Loop.count=2 org.pcap4j.sample.Loop icmp

The point is you have to specify org.pcap4j.core.pcapLibName Java property, because the file name of libpcap is different from what pcap4j is expected (libpcap.so).

 

[Note]

I don't know why IP address information is not displayed.

-------------

    (snip)

NIF[0]: en0

          : description: null

NIF[1]: p2p0

          : description: null

NIF[2]: lo0

          : description: null

    (snip)

-------------

 

[Detail version information]

  • Mac OS X 10.8.2
  • Java 1.6.0 Update 37
  • libpcap 1.3.0
  • jna 3.3.0
  • slf4j-api 1.7.2