0

I have an ant project, Here is the build.xml

<project name="NAME" default="compile" basedir=".">

  <!-- set global properties for this build -->
  <property name="src" value="src/main/java" />
  <property name="src-test" value="src/test/java" />
  <property name="lib" value="lib" />
  <property name="build" value="build" />
  <property name="dist" value="dist" />

  <!-- general classpath definition, incl. CLASSPATH env. variable,
   // but jars in lib directory have precedence over the CLASSPATH variable -->
  <path id="project.class.path">
    <fileset dir="${lib}">
      <include name="*.jar" />
      <include name="*.zip" />
    </fileset>
    <pathelement location="${build}/classes" />
    <pathelement location="${build}/testcases" />
    <pathelement path="${java.class.path}" />
    <!-- Import all dependencies -->
    <pathelement path="${lib}/*.jar" />
  </path>
  <target name="compile" depends="init_compile"
    description="Compile package and deposit class files in build/classes">
    <javac includeantruntime="false" srcdir="${src}"
      fork="yes" memoryMaximumSize="${javac_max_memory}"
      destdir="${build}/classes"
      optimize="${optimization}"
      debug="${debug}"
      deprecation="${deprecation}"
      source="11" target="11">
      <!-- Import lib dependencies -->
      <classpath>
        <fileset dir="${lib}">
          <include name="*.jar" />
          <include name="*.zip" />
        </fileset>
      </classpath>
    </javac>
    <copy todir="${build}/classes">
      <fileset dir="${src}">
        <include name="**/*.txt" />
        <include name="**/*.xml" />
      </fileset>
    </copy>
  </target>
</project>

Here is the Java Code:

public static void main(String[] args) {
        // Specify the pcap file to read
        String pcapFilePath = "./capture.pcap";

        // Open the pcap file for reading
        Pcap pcap = Pcap.openOffline(pcapFilePath, new StringBuilder());

        // Loop through all packets in the file and print out their data
        PcapPacket packet = new PcapPacket();
        while (pcap.nextEx(packet) == Pcap.NEXT_EX_OK) {
            System.out.println(packet);
        }

        // Close the pcap file
        pcap.close();
    }

And my architecture look like that :

├── build.xml
├── lib
│   ├── all-1.1.2.pom
│   ├── jnetpcap.jar
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── project
    │           └── NAME.java
    └── test ...

But when I go for ant compile, here is the error message:

error: cannot find symbol Pcap

I use java11 and I downloaded the jnetpcap jar on their github but ant can't seem to find it . If someone knows what's wrong with my project it could be a nice help

SanjiSqurt
  • 75
  • 7
  • I am not a subject matter expert here but try these steps and see if it helps: The following line seems to be incorrect and should be removed: Correct way to do it as you’ve already done it on the upper lines: 1. Download the jNetPcap JAR file and place it in your project's "lib" directory. 2. Check your build.xml file to ensure it's including JAR files from the "lib" directory in the classpath. – Roshan Khadka Apr 12 '23 at 19:09
  • I removed the line as you say, I have already the jar in my lib directory as I show in the architecture of my project. I think it is including it here : But I still have the error – SanjiSqurt Apr 12 '23 at 19:22
  • I've just wrote an answer, not sure if it would help but please try. Good Luck! – Roshan Khadka Apr 13 '23 at 02:22

1 Answers1

0

Maybe worth a shot if you haven't already tried yet. Please replace you java file to something similar.

import org.jnetpcap.Pcap; import org.jnetpcap.packet.PcapPacket;

public class PcapReader {

public static void main(String[] args) {
    // Specify the pcap file to read
    String pcapFilePath = "./capture.pcap";

    // Open the pcap file for reading
    StringBuilder errorMessage = new StringBuilder();
    Pcap pcap = Pcap.openOffline(pcapFilePath, errorMessage);

    // Check if there was an error while opening the pcap file
    if (pcap == null) {
        System.err.println("Error while opening pcap file: " + errorMessage);
        return;
    }

    // Loop through all packets in the file and print out their data
    PcapPacket packet = new PcapPacket();
    while (pcap.nextEx(packet) == Pcap.NEXT_EX_OK) {
        System.out.println(packet);
    }

    // Close the pcap file
    pcap.close();
}

}

Roshan Khadka
  • 519
  • 1
  • 4
  • 6