-
Notifications
You must be signed in to change notification settings - Fork 9
1. Getting Started
This page walks you through setting up development using jNetPcap module for packet capture, network statistics and packet transmission using libpcap library.
- Java 19 or higher with --preview-enable option (for Foreign Function feature JEP 424)
- Native libpcap library installed on your system
- libpcap-dev on Linux systems
- WinPcap or Npcap on Microsoft Windows systems
- Note that jNetPcap does not have any other external to JRE module dependencies. It is a stand-along module.
There several ways to install org.jnetpcap
module
- add a Maven dependency to your pom.xml file
<dependency>
<groupId>org.jnetpcap</groupId>
<artifactId>jnetpcap</artifactId>
<version>2.0.0-preview.1</version>
</dependency>
- Download the Jar file from our download page: UNDER CONSTRUCTION
- Clone github repo and build from source
Without going into too much detail about native libpcap installation, as that is covered under 'tcpdump.org' and 'npcap.com' websites, here are a few tips as they relate to 'maven' build script and how the native libpcap is located and loaded by jNetPcap.
Nothing special needs to be done for linux and Windows platforms. The maven pom.xml defines numerous profiles to handle specific directory locations for each of those.
Mac platforms provide two main installation methods for installation of libpcap library.
Howbrew installation tool, installs libpcap library under the directory /usr/local/Cellar/libpcap/${VERSION}/lib
. There is no default 'latest' installation directory and you must always specify a libpcap version when setting up your application or using maven unit tests.
If you are setting up your application from java command line or in IDE, you just have to define the system property java.library.path
to point to the directory where libpcap.dylib
shared object resides.
If you are trying to run the maven unit tests, you must specify the user property with maven command line -Dlibpcap.version=X.Y.Z
as an example, mvn -Dlibpcap.version=1.10.3 test
.
When installing libpcap library using Mac Ports, things are a little easer since Mac Ports libpcap package gets installed in a non-versioned directly and the libpcap.dylib
file found there is a soft link to the latest version.
Note: To install libpcap using Mac Ports
sudo port install libpcap
In your Java project, you need to add the following requires dependency to have jNetPcap on the module-path of your project. In your module-info.java
file at the root of of your project directory add:
module my.project.module {
requires org.jnetpcap;
}
In order to properly run org.jnetpcap
module you need to add a couple of command line flags to your application startup and/or build system.
- java needs to be able to find the install native libpcap library
-Djava.library.path=$(absolute_path_to_libpcap)
- For Java 19 and until later released fully incorporate Foreign Function feature, you need to add to the startup and/or build system enable
--enable-features
- For running jUnit5 tests, the command line
mvn test
should suffice. If manually setting up, note that thesrc/test/java
source directory needs to be outside the module system, as unnamed module, and jUnit5 module added to the java'sclasspath
not module-path.
Note 1 that unit tests utilize multiple tags and the default maven configuration is setup to only run tests which require normal user-permission
and exclude windows-api|unix-api|linux-api
, which leaves only tests which can run under regular user privileges and are well supported libpcap-api
tests common to all platforms. Please see javadocs for the org.jnetpcap.test
package for details on available tags.
Note 2 the unit tests source code found in the github repo are a good source of examples on every single jNetPcap API call.
jNetPcap version 2 - libpcap Java library