Pcap4J is a Java library for capturing, crafting and sending packets. Pcap4J wraps a native packet capture library (libpcap, Npcap, or WinPcap) via JNA and provides you Java-Oriented APIs.
Pcap4J is available on the Maven Central Repository.
Pcap4j needs JRE 8+. And also a pcap native library (libpcap 1.0.0+, Npcap or WinPcap 3.0+), JNA 5+, slf4j-api, and an implementation of logger for slf4j are required. I'm using the following libraries for the test.
I tested Pcap4j on the following OSes with x86 or x64 processors.
Hopefully Pcap4j can run on the other OSes supported by both JNA and libpcap.
Pcap4J needs administrator/root privileges.
Or, if on Linux, you can run Pcap4J with a non-root user by granting capabilities CAP_NET_RAW and CAP_NET_ADMIN
to your java command by the following command: setcap cap_net_raw,cap_net_admin=eip /path/to/java
The latest JavaDoc is here. Each version's JavaDoc is on the Maven Central Repository.
Refer to here for information about Pcap4J modules.
Because Pcap4J is a wrapper of a pcap native library, the following documents help you to understand how to use Pcap4J.
You can learn how to write Pcap4J programs from samples.
Learn more about Pcap4j from the following documents:
See the following examples:
If you want to run a sample in pcap4j-sample on Eclipse, add pcap4j-packetfactory-static or pcap4j-packetfactory-propertiesbased project to the top of User Entries in Classpath tab of the Run Configuration for the sample.
Gradle
Add a dependency to the build.gradle as like below:
dependencies {
compile 'org.pcap4j:pcap4j-core:2.0.0-alpha.6'
compile 'org.pcap4j:pcap4j-packetfactory-static:2.0.0-alpha.6'
}
Maven
Add a dependency to the pom.xml as like below:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<dependencies>
<dependency>
<groupId>org.pcap4j</groupId>
<artifactId>pcap4j-core</artifactId>
<version>2.0.0-alpha.6</version>
</dependency>
<dependency>
<groupId>org.pcap4j</groupId>
<artifactId>pcap4j-packetfactory-static</artifactId>
<version>2.0.0-alpha.6</version>
</dependency>
...
</dependencies>
...
</project>
By default, Pcap4j loads the native libraries on the following conditions:
PATH environment variable, etc. (See MSDN for the details.), and %SystemRoot%\System32\Npcap.LD_LIBRARY_PATH environment variable)DYLD_LIBRARY_PATH environment variable)You can use the following Java system properties to change the default behavior.
There are two native pcap libraries for Windows; WinPcap and Npcap.
The development of WinPcap has stopped since version 4.1.3 (libpcap 1.0.0 base) was released on 3/8/2013, while Npcap is still being developed. So, you should pick Npcap if you want to use new features or so.
By default, WinPcap is installed in %SystemRoot%\System32\, and Npcap is installed in %SystemRoot%\System32\Npcap\.
If you have both WinPcap and Npcap installed and want to use Npcap, you can explicitly specify it by setting org.pcap4j.core.pcapLibName to %SystemRoot%\System32\Npcap\wpcap.dll and org.pcap4j.core.packetLibName to %SystemRoot%\System32\Npcap\Packet.dll.
A Docker image for Pcap4J on CentOS is available at Docker Hub.
Download it by docker pull kaitoy/pcap4j and execute docker run kaitoy/pcap4j:latest to start capturing packets from eth0 on the container.
This image is built everytime a commit is made on the Git repositry.
Install libpcap, WinPcap, or Npcap:
Install Npcap or WinPcap (if Windows), or libpcap (if Linux/UNIX). It's needed for the unit tests which are run during a build.
Install JDK:
Download and install JDK 9, 10, 11, or 12, and set the environment variable JAVA_HOME properly.
Install Git:
Download Git and install it. This step is optional.
Clone the Pcap4J repository:
If you installed Git, execute the following command: git clone [email protected]:kaitoy/pcap4j.git
Otherwise, download the repository as a zip ball and extract it.
Build:
Open a command prompt/a terminal, cd to the project root directory,
and execute gradlew build.
Note Administrator/root privileges are needed for the unit tests.
Fork this repository.
Create a branch from v1 branch.
Write code.
gradlew spotlessApplySend a PR from the branch.
Kaito Yamada ([email protected])
Content type
Image
Digest
sha256:865b5de27…
Size
613 MB
Last updated
almost 7 years ago
docker pull kaitoy/pcap4j