"错误读取转储文件:权限被拒绝" Windows上的Jnetpcap API

时间:2015-06-29 14:00:39

标签: java windows pcap jnetpcap

我使用Jnetpcap 1.3.0版本来提取pcap文件。

以下是我的代码段

  /* 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());

     }
  }
  }

但是在eclipse(Windows)上运行时,我收到此错误

&#34;错误读取转储文件:权限被拒绝&#34;

我也包含了.dll文件,但似乎无法理解这里的问题。

注意 - (此代码在Ubuntu上运行正常)

1 个答案:

答案 0 :(得分:0)

用户无法访问此文件,或者该文件由另一个进程独占打开。

// 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);

输出 - 文件不存在

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

输出 - 普通用户没有访问权限

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

您可以使用cacls no-gre-sample.pcap

检查权限
C:\test\no-gre-sample.pcap BUILTIN\Users:N

表示组Users中的所有用户(而不是更高权限的用户)对此文件没有权限。但您还需要检查目录权限。

遗憾的是,Pcap.openOffline报告的错误对于missed permissionsread locked by another application两种情况都是相同的。您可以运行一个简单的测试来查看差异。

InputStream is = new FileInputStream(filePath);
is.read();
is.close();
错误许可

输出

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

其他应用程序锁定的读取输出

 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)