JDivert is a powerful Java binding for WinDivert, a Windows driver that allows user-mode applications to capture, modify, and drop network packets sent to or from the Windows network stack.
Capture and re-inject all TCP traffic on port 80:
try (WinDivert w = new WinDivert("tcp.DstPort == 80").open()) {
while (true) {
Packet packet = w.recv();
packet.getTcp().ifPresent(tcp -> {
System.out.println("Captured TCP packet to port: " + tcp.getDstPort());
});
w.send(packet);
}
}For more complex scenarios, see our Examples Guide.
- Operating System: Windows (64-bit).
- Privileges: Administrator privileges are required to load the WinDivert driver and open capture handles.
- Java: Version 8 or higher.
- Always use try-with-resources: JDivert manages native handles and buffers. The
WinDivertandWinDivertAsyncResultclasses implementAutoCloseableto ensure these resources are released. - Filter specifically: Use the Filter Language to capture only the traffic you need. This happens in kernel-mode and is significantly faster than filtering in Java.
- Handle Re-injection: When you receive a packet with
recv(), it is removed from the network stack. If you don't callsend(), the packet is dropped.
- Maven 3.9+
- Vagrant & VirtualBox (Required for full test execution)
Due to the nature of the WinDivert driver and its requirement for specific network stack interactions and Administrator privileges, the full test suite is designed to be executed within a clean, isolated Windows 11 environment managed by Vagrant.
- Boot the VM:
vagrant up
- Sync and Execute:
Run the following commands to sync the latest code to a local directory in the VM (avoiding synced folder permission issues) and execute the tests:
vagrant winrm --command "robocopy C:\jdivert C:\local_jdivert /MIR /XD .git .vagrant target" vagrant winrm --command "cd C:\local_jdivert; mvn clean verify"
Note: While you can compile the project on any OS, actual packet capture tests will only succeed in the provided Vagrant environment or an elevated Windows session.
JDivert bridges the gap between Java and the native WinDivert C library using JNA (with optional Project Panama support on Java 22+).
- Zero-Copy: Leverages direct buffers to process packets without redundant memory copying.
- Memory-Safe: Employs deterministic cleanup to prevent native memory leaks.
- Zero-Install: WinDivert binaries are bundled and extracted automatically into versioned temporary directories.
Read the full Architecture Overview for more details.
<dependency>
<groupId>com.github.ffalcinelli</groupId>
<artifactId>jdivert</artifactId>
<version>3.0.0</version>
</dependency>implementation 'com.github.ffalcinelli:jdivert:3.0.0'JDivert is a Java wrapper around the excellent WinDivert project created by basil00. We would like to thank the WinDivert community for providing such a powerful tool for network manipulation on Windows.
- Full API Reference (Javadoc)
- Architecture Overview
- Filter Language Guide
- Examples Guide
- Performance Considerations
- Troubleshooting Guide
- Security Policy
JDivert is dual-licensed under LGPL-3.0-or-later and GPL-2.0-or-later.
Refer to the LICENSE, LICENSE-LGPL-3.0-or-later, and LICENSE-GPL-2.0-or-later files for the full license text.