java and pcap4j on linux Problem to load class

Mihail HRS :

I have tried to write a program using pcap4j for java. I have downloaded and built it. Then I wrote this code using maven:

import java.net.*;

import org.pcap4j.core.*;
import org.pcap4j.core.PcapNativeException;


/**
 * Hello world!
 *
 */
public class App {
    public static void main(String[] args) {
        InetAddress addr;

        try {
            addr = InetAddress.getByName("192.168.10.100");
            PcapNetworkInterface nif = Pcaps.getDevByAddress(addr);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (PcapNativeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println( "Hello World!" );
    }
}

Maven file:

<?xml version="1.0" encoding="UTF-8"?>

<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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.creatorkhr</groupId>
  <artifactId>test</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>test</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.pcap4j</groupId>
      <artifactId>pcap4j-core</artifactId>
      <version>1.8.2</version>
    </dependency>
    <dependency>
      <groupId>org.pcap4j</groupId>
      <artifactId>pcap4j-packetfactory-static</artifactId>
      <version>1.8.2</version>
    </dependency>
</dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
          <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.creatorkhr.test.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <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>
    </pluginManagement>
  </build>
</project>

I use the standard maven hierarchy

Then I compiled it using:

$mvn package 

And have no error.
But when I tried to run it using:

java -jar target/test-1.0-SNAPSHOT.jar

I got this output:

Error: Unable to initialize main class com.creatorkhr.test.App
Caused by: java.lang.NoClassDefFoundError: org/pcap4j/core/PcapNativeException

I have tried to compile with other imported classes but still got java.lang.NoClassDefFoundError: org/pcap4j/core/*"class_I_tring_to_compile_with!"*

S. MS :

You get the error because your jar file only contains the App.class itself but not all the dependencies. You can include all the pcap4j jars, your program depends on with maven assembly plugin. You have to place the following configuration inside of <build> ... <plugins> section of your pom.xml. Attention not inside <pluginManagement> section !!!

     <build>
       <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>
                                        com.creatorkhr.test.App
                                    </mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    ...
    ...
    </build>

and then execute

mvn clean package

in your target directory you'll find a file test-1.0-SNAPSHOT-jar-with-dependencies.jar which contains the App.class and all necessary pcap4j jars. And you start your program with

java -jar target/test-1.0-SNAPSHOT-jar-with-dependencies.jar

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=20920&siteId=1