Maven与vs代码在windows pcap4j

pod7payv  于 2023-10-17  发布在  Maven
关注(0)|答案(1)|浏览(135)

我想在一个java项目中使用vs code + maven
我安装了Java扩展插件/Java扩展包v0.25.14 /Java Maven/Java项目管理器/Java测试运行器
maven扩展编译成功完成,但在vs代码中,我看到红色的导入行
我得到一个错误“导入org.pcap4j无法解决”行:import org.pcap4j.core.pcapHandle;
这是我的pom.xml

<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.example</groupId>
  <artifactId>demo</artifactId>
  <version>1.0</version>

  <name>demo</name>
  <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>

  <repositories>
    <repository>
      <id>Central</id>
      <url>https://repo1.maven.org/maven2/</url>
    </repository>
  </repositories>
  

  

  <dependencies>
    <dependency>
      <groupId>org.pcap4j</groupId> 
      <artifactId>pcap4j</artifactId>
      <version>2.0.0-alpha.6</version>
      <type>pom</type>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </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>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </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>
      </plugins>
    </pluginManagement>
  </build>
</project>

这是我的代码

package com.example;
import org.pcap4j.core.PcapHandle;
import org.pcap4j.core.Pcaps;
import org.pcap4j.packet.Packet;

import java.io.File;
import java.io.IOException;

public class App {

    public static void main(String[] args) throws IOException {
        String inputPcapPath = "input.pcap";
        String outputPcapPath = "output.pcap";

        PcapHandle handle = Pcaps.openOffline(inputPcapPath);
        PcapHandle outputHandle = Pcaps.openOffline(inputPcapPath); // Open another handle for writing

        while (true) {
            try {
                Packet packet = handle.getNextPacketEx();
                // Check if it's an IP packet with UDP and GSM SMS data (adjust the filters as needed)
            
            } catch (IOException e) {
                break; // No more packets
            }
        }

        handle.close();
        outputHandle.close();
    }

  
}
e4yzc0pl

e4yzc0pl1#

您应该使用:用途:

<dependency>
  <groupId>org.pcap4j</groupId> 
  <artifactId>pcap4j</artifactId>
  <version>2.0.0-alpha.6</version>
</dependency>

而不是:

<dependency>
  <groupId>org.pcap4j</groupId> 
  <artifactId>pcap4j</artifactId>
  <version>2.0.0-alpha.6</version>
  <type>pom</type>
</dependency>

您也可以删除该部分:

<repositories>
    <repository>
      <id>Central</id>
      <url>https://repo1.maven.org/maven2/</url>
    </repository>
  </repositories>

另外,我想问你为什么使用Java 7,因为Java 7是2011年的!使用最新版本的Java。
此外,我强烈建议将所有插件版本升级到最新版本(https://maven.apache.org/plugins/)。
更新:项目https://github.com/kaitoy/pcap4j看起来像被遗弃了。

相关问题