javamavenpcappcap4j

Pcap4J Error:java.lang.ClassNotFoundException: org.pcap4j.util.NifSelector while trying to run my code to list out the available interfaces


I'm currently trying to run a Java code through Maven to make a packet sniffing tool. Currently I'm running a simple code to select all the currently available network interfaces using NifSelector but im running into a the following error -> Exception in thread "main" java.lang.NoClassDefFoundError: org/pcap4j/util/NifSelector. This suggests that the class wasn't found, obviously, but I cannot find anything in the documentation or on SO that has a rectification for this error. I have the jar file for pcap4j and I've added it as a dependency in my pom.xml. I also installed npcap on my windows machine(this setup is running on windows).

import org.pcap4j.util.NifSelector;

public class App 
{
    public static void main( String[] args )
{
    PcapNetworkInterface device = null;
    try{
        device = new NifSelector().selectNetworkInterface();
    }catch(IOException e){
        e.printStackTrace();
    }
    System.out.println( "Your choice: " + device);
}
}

Above is the code that im trying to run with the required import statement for the NifSelector class too. https://github.com/kaitoy/pcap4j Is a link to the documentation for the project. All examples used in the docs do not have any issue with NifSelector. Any help would be greatly appreciated!

EDIT: Added pom.xml snippet

<dependency>
        <groupId>org.pcap4j</groupId>
        <artifactId>pcap4j-core</artifactId>
        <version>1.8.2</version>
        <type>jar</type>
    </dependency>

    <dependency>
        <groupId>org.pcap4j</groupId>
        <artifactId>pcap4j-packetfactory-static</artifactId>
        <version>1.8.2</version>
    </dependency>

Pom.xml snippet for the shader plugin

<!-- Embed dependencies inside the final JAR -->
                    <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-shade-plugin</artifactId>
                            <version>3.1.0</version>
                            <executions>
                                    <execution>
                                            <phase>package</phase>
                                            <goals>
                                                    <goal>shade</goal>
                                            </goals>
                                    </execution>
                            </executions>
                            <configuration>
                                    <finalName>new-$1.0-SNAPSHOT</finalName>
                            </configuration>
                    </plugin>

Solution

  • You probably didn't include all dependencies inside the final JAR. Double check that, as said in the guide you mention, the maven-shade-plugin is correctly executed.

    <build>
       <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <finalName>uber-${project.artifactId}-${project.version}</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>