java 和 pcap4j 关于 linux 加载问题 class

java and pcap4j on linux Problem to load class

我曾尝试使用 pcap4j 为 java 编写程序。我已经下载并构建了它。然后我使用 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 文件:

<?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>

我使用标准的 Maven 层次结构

然后我编译它使用:

$mvn package 

并且没有错误。
但是当我尝试使用 运行 它时:

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

我得到了这个输出:

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

我尝试用其他导入的 类 进行编译,但仍然得到 java.lang.NoClassDefFoundError: org/pcap4j/core/*"class_I_tring_to_compile_with!"*

您收到错误是因为您的 jar 文件仅包含 App.class 本身,而不包含所有依赖项。您可以包含所有 pcap4j jar,您的程序依赖于 maven 程序集插件。您必须将以下配置放在 pom.xml 的 <build> ... <plugins> 部分中。注意 不在 <pluginManagement> 部分 !!!

     <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>

然后执行

mvn clean package

在您的目标目录中,您会找到一个文件 test-1.0-SNAPSHOT-jar-with-dependencies.jar,其中包含 App.class 和所有必需的 pcap4j jar。然后你用

开始你的程序
java -jar target/test-1.0-SNAPSHOT-jar-with-dependencies.jar