0

I'm using Jnetpcap 1.3.0 version for extracting the pcap file.

Below is my code snippet

  /* Main Class */
    public class Proceed{

 public static void main(String[] args) {

  PCapFile pcapFile = new PCapFile("C:/test/no-gre-sample.pcap");
  pcapFile.process();

  }
 }

 /in some other class I have written this method */

  public void process() {
  RandomAccessFile raf = null;
  FileLock lock = null;
  try {
     raf = new RandomAccessFile(file, "rw");
     lock = raf.getChannel().tryLock();

     this.pcap = Pcap.openOffline(file, errbuf);
     System.out.printf("Opening file for reading: %s", filePath);
     if (pcap == null) {
        System.err.println(errbuf); // prob occurs here
     } else {
        PcapPacketHandler<String> jpacketHandler;
        jpacketHandler = new PcapPacketHandler<String>() {
           @Override
           public void nextPacket( packet, String user) {
              PPacket pcap = new PPacket(packet, user);
             //Process packet
           }
        };

        // Loop over all packets in the file...
        try {
           pcap.loop(-1, jpacketHandler, "jNetPcap Rocks!!!!!");
        } finally {
           pcap.close();
        }
     }
  } catch (IOException e) {
     System.err.println(e.getMessage());

  } finally {
     try {
        if (lock != null) {
           lock.release();
        }
        if (raf != null) {
           raf.close();
        }
     } catch (IOException e) {
        System.err.println(e.getMessage());

     }
  }
  }

But while running on eclipse(Windows) I'm getting this error

"error reading dump file: Permission denied"

I have included the .dll file as well , but cant seem to understand what is the issue here.

Note - (This code works fine on Ubuntu)

user102964
  • 21
  • 3
  • 1
    Have you checked that the user who runs the Java application has the necessary permissions on Windows to read the file? – SubOptimal Jun 29 '15 at 14:05
  • Im running eclipse as Administrator , still getting the same error ! , also I tried running the jar as Administrator on command prompt , still the same error ! – user102964 Jun 29 '15 at 14:24
  • Could you please post the code which is raising the above error message. It's seem it's not from the Jnetpcap library. (could not find it in the current source code) – SubOptimal Jun 29 '15 at 14:24
  • Is the file in use ? Try to display it with `type`, although it will output garbage you can check if it is readable – Marged Jun 29 '15 at 14:33
  • No the file is not in use ! and Yes the file is readable , as Im opening it with wireshark. ! – user102964 Jun 29 '15 at 14:44

1 Answers1

0

Either the user has no access to this file or the file is exclusive open by another process.

// simple code to check
String filePath = "C:/test/no-gre-sample.pcap";
StringBuilder errbuf = new StringBuilder();
Pcap pcap = Pcap.openOffline(filePath, errbuf);
System.out.println("errbuf = " + errbuf);
System.out.println("pcap = " + pcap);

output - file not exist

errbuf = C:/test/no-gre-sample.pcap: No such file or directory
pcap = null

output - normal user has no access permission

errbuf = C:/test/no-gre-sample.pcap: Permission denied
pcap = null

You can check the permission with cacls no-gre-sample.pcap

C:\test\no-gre-sample.pcap BUILTIN\Users:N

means all users within the group Users (and not in a more privileged one) have no permissions for this file. But you need to check the directory permissions as well.

Sadly the error reported by Pcap.openOffline is the same for both cases missed permissions and read locked by another application. You can run a simple test to see the difference.

InputStream is = new FileInputStream(filePath);
is.read();
is.close();

output for missed permission

Exception in thread "main" java.io.FileNotFoundException: _
    C:\test\no-gre-sample.pcap (Access is denied)

output for read locked by another application

 Exception in thread "main" java.io.FileNotFoundException: _
    C:\test\no-gre-sample.pcap _
    (The process cannot access the file because it is being used by another process)
SubOptimal
  • 22,518
  • 3
  • 53
  • 69