Java &引用;读取转储文件时出错:权限被拒绝";windows上的Jnetpcap API
我正在使用Jnetpcap 1.3.0版本来提取pcap文件 下面是我的代码片段Java &引用;读取转储文件时出错:权限被拒绝";windows上的Jnetpcap API,java,windows,pcap,jnetpcap,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
/* 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());
}
}
}
/*主类*/
公开课开始{
公共静态void main(字符串[]args){
PCapFile PCapFile=新的PCapFile(“C:/test/no gre sample.pcap”);
pcapFile.process();
}
}
/在另一节课上,我写了这个方法*/
公共程序(){
RandomAccessFile raf=null;
FileLock=null;
试一试{
raf=新随机访问文件(文件“rw”);
lock=raf.getChannel().tryLock();
this.pcap=pcap.openOffline(文件,errbuf);
System.out.printf(“打开文件读取:%s”,文件路径);
如果(pcap==null){
System.err.println(errbuf);//此处出现问题
}否则{
PcapPacketHandler jpacketHandler;
jpacketHandler=新的PcapPacketHandler(){
@凌驾
public void nextPacket(数据包、字符串用户){
PPacket pcap=新的PPacket(数据包,用户);
//进程包
}
};
//循环文件中的所有数据包。。。
试一试{
loop(-1,jpacketHandler,“jNetPcap岩石!!!”;
}最后{
pcap.close();
}
}
}捕获(IOE异常){
System.err.println(e.getMessage());
}最后{
试一试{
if(lock!=null){
锁定。释放();
}
如果(raf!=null){
raf.close();
}
}捕获(IOE异常){
System.err.println(e.getMessage());
}
}
}
但是在eclipse(Windows)上运行时,我遇到了这个错误
“读取转储文件时出错:权限被拒绝”
我也包括了.dll文件,但似乎无法理解这里的问题是什么
注意-(此代码在Ubuntu上运行良好)用户无权访问此文件,或者该文件被其他进程独占打开
// 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
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)
您可以使用cacls no gre sample.pcap检查权限
C:\test\no-gre-sample.pcap BUILTIN\Users:N
表示组用户
(而不是特权更高的用户)中的所有用户都没有此文件的权限。但是您还需要检查目录权限
遗憾的是,Pcap.openOffline
报告的错误对于另一个应用程序所锁定的丢失权限和读取权限都是相同的。您可以运行一个简单的测试来查看差异
InputStream is = new FileInputStream(filePath);
is.read();
is.close();
丢失权限的输出
errbuf = C:/test/no-gre-sample.pcap: Permission denied
pcap = null
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)
另一个应用程序锁定的读取输出
errbuf = C:/test/no-gre-sample.pcap: Permission denied
pcap = null
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)
您是否已检查运行Java应用程序的用户是否具有在Windows上读取文件所需的权限?我以管理员身份运行eclipse,仍然会收到相同的错误,此外,我还尝试在命令提示符下以管理员身份运行jar,仍然是相同的错误!请您发布引发上述错误消息的代码。看起来它不是来自Jnetpcap库。(在当前源代码中找不到)该文件正在使用中吗?尝试使用类型显示它,尽管它会输出垃圾。您可以检查它是否可读。否文件未被使用!是的,这个文件是可读的,因为我用wireshark打开它!