SanjiSqurt
SanjiSqurt

Reputation: 87

Use JNetPcap with Ant

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

Upvotes: 0

Views: 66

Answers (1)

Roshan Khadka
Roshan Khadka

Reputation: 736

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();
}

}

Upvotes: 0

Related Questions