1

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>
need_to_know_now
  • 328
  • 1
  • 2
  • 13

1 Answers1

0

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>
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • I will add the pm.xml snippet where i have added the shade plugins as an edit to my question. It looks fine to me. – need_to_know_now Sep 16 '19 at 06:50
  • Okay i found multiple jars in my target folder. I ran the one which was named as 'check-1.0-SNAPSHOT-shaded.jar' and it worked. I don't know what or how it happened, because i didn't repackage it using 'mvn package' I guess i was just using the wrong jar? Could you please enlighten me if that is a correct assumption? – need_to_know_now Sep 16 '19 at 06:57
  • Yes, you will find in the target directory one jar embedding the others. This is the only one that is directly executable with `java -jar`. – Ortomala Lokni Sep 16 '19 at 07:35
  • So in the link that i mentioned above, whose tutorial i was following, why wasn't it told to run the "xyz-shaded.jar"? Ive been splitting my hair over this for about a week now. – need_to_know_now Sep 16 '19 at 14:13
  • If you follow carefully the tutorial, your jar will have the name `uber-pcap-1.0.0.jar` and you can run it with : `sudo java -jar target/uber-pcap-1.0.0.jar` (privileges are needed to capture on the network interface) – Ortomala Lokni Sep 16 '19 at 15:13
  • Yeah thats the problem, i didnt have any file named "uber-pacap-1.0.0.jar". Infact i still don't. is it something to do with the fact that I'm running it in windows? – need_to_know_now Sep 17 '19 at 13:25