Java JNetPcap项目

Java JNetPcap项目,java,libpcap,jnetpcap,Java,Libpcap,Jnetpcap,我制作了一个jNetPcap项目,其代码如下: 当我尝试执行该程序时,我在控制台标记中得到以下结果: 无法读取设备列表,错误为 由于jNetPcap依赖于需要root权限的Libpcap C库,很明显这就是我的程序无法运行的原因。有人能帮我解决这个问题吗 注意:我试图在src文件夹中创建.jar文件,并从该.jar文件创建一个.exe,但它会引发以下错误: Exception in thread "main" java.lang.NoClassDefFoundError: o

我制作了一个jNetPcap项目,其代码如下:

当我尝试执行该程序时,我在控制台标记中得到以下结果:

无法读取设备列表,错误为

由于jNetPcap依赖于需要root权限的Libpcap C库,很明显这就是我的程序无法运行的原因。有人能帮我解决这个问题吗

注意:我试图在src文件夹中创建.jar文件,并从该.jar文件创建一个.exe,但它会引发以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/jnetpcap/packet/PcapPacketHandler
Caused by: java.lang.ClassNotFoundException: org.jnetpcap.packet.PcapPacketHandler
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Pcap_capture. Program will exit.

import java.io.DataInputStream;
导入java.io.DataOutputStream;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
导入org.jnetpcap.Pcap;
导入org.jnetpcap.PcapIf;
导入org.jnetpcap.packet.PcapPacket;
导入org.jnetpcap.packet.PcapPacketHandler;
/** 
*以下是此示例生成的输出:
*  
*找到的网络设备:
*#0:\Device\NPF{BC81C4FC-242F-4F1C-9DAD-EA9523CC992D}[Intel(R)PRO/100ve]
*#1:\Device\NPF{E048DA7F-D007-4EEF-909D-4238F6344971}[VMware虚拟以太网适配器]
*#2:\Device\NPF{5B62B373-3EC1-460D-8C71-54AA0BF761C7}[VMware虚拟以太网适配器]
*#3:\Device\NPF_GenericDialupAdapter[用于一般拨号和VPN捕获的适配器]
*  
*代表您选择“英特尔(R)PRO/100 VE”:
*2009年11月3日星期二18:52:42东部标准时间caplen=1362 len=1362 jNetPcap rocks收到数据包!
*2009年11月3日星期二18:52:45东部标准时间收到数据包caplen=82 len=82 jNetPcap rocks!
*2009年11月3日星期二18:52:45东部标准时间收到数据包caplen=145 len=145 jNetPcap rocks!
*2009年11月3日星期二18:52:45东部标准时间收到数据包caplen=62 len=62 jNetPcap rocks!
*2009年11月3日星期二18:52:45美国东部时间caplen=164 len=164 jNetPcap rocks收到数据包!
*2009年11月3日星期二18:52:45东部标准时间收到数据包caplen=62 len=62 jNetPcap rocks!
*2009年11月3日星期二18:52:45美国东部时间caplen=54 len=54 jNetPcap rocks收到数据包!
*2009年11月3日星期二18:52:45东部标准时间caplen=1073 len=1073 jNetPcap rocks收到数据包!
*2009年11月3日星期二18:52:45东部标准时间收到数据包caplen=1514 len=1514 jNetPcap rocks!
*2009年11月3日星期二18:52:45东部标准时间caplen=279 len=279 jNetPcap rocks收到数据包!
*/  
公共类Pcap_捕获{
/** 
*主启动方法
*  
*@param args
*忽略
*@抛出异常
*/  
公共静态void main(字符串[]args)引发IOException{
List alldevs=new ArrayList();//将用NIC填充
StringBuilder errbuf=new StringBuilder();//对于任何错误msgs
布尔retval=false;
/*************************************************************************** 
*首先获取此系统上的设备列表
**************************************************************************/    
Process supprocess=Runtime.getRuntime().exec(“su”);
DataOutputStream os=新的DataOutputStream(supprocess.getOutputStream());
DataInputStream是=新的DataInputStream(supprocess.getInputStream());
if(null!=os&&null!=is)
{
//正在获取当前用户的id以检查此用户是否为root用户
os.writeBytes(“id\n”);
os.flush();
字符串currUid=is.readLine();
布尔exitSu=false;
if(null==currUid)
{
retval=false;
exitSu=假;
}
else if(true==currUid.contains(“uid=0”))
{
retval=true;
exitSu=真;
}
其他的
{
retval=false;
exitSu=真;
}
进出口银行
{
os.writeBytes(“退出”);
os.flush();
}
}
int r=Pcap.findAllDevs(alldevs,errbuf);
如果(r==Pcap.NOT|u OK | | alldevs.isEmpty()){
System.err.printf(“无法读取设备列表,错误为%s”),errbuf
.toString());
回来
}  
System.out.println(“找到的网络设备:”);
int i=0;
对于(PcapIf设备:alldevs){
字符串说明=
(device.getDescription()!=null)?device.getDescription()
:“没有可用的描述”;
System.out.printf(“#%d:%s[%s]\n”,i++,device.getName(),description);
}  
PcapIf device=alldevs.get(0);//我们知道至少有1个设备
系统输出
.printf(“\n代表您选择'%s:\n”,
(device.getDescription()!=null)?device.getDescription()
:device.getName());
/*************************************************************************** 
*其次,我们打开所选设备
**************************************************************************/  
int snaplen=64*1024;//捕获所有数据包,无指令
int flags=Pcap.MODE\u PROMISCUOUS;//捕获所有数据包
int timeout=10*1000;//以毫秒为单位的10秒
Pcap Pcap=
openLive(device.getName(),snaplen,标志,超时,errbuf);
如果(pcap==null){
System.err.printf(“打开设备进行捕获时出错:”
+errbuf.toString());
回来
}  
/*************************************************************************** 
*第三,我们创建一个数据包处理程序,它将从
*libpcap循环。
**************************************************************************/  
PcapPacketHandler jpacketHandler=新的PcapPacketHandler(){
public void nextPacket(PcapPacket数据包,字符串用户){
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
import org.jnetpcap.packet.PcapPacket;
import org.jnetpcap.packet.PcapPacketHandler;
  
/** 
 * Here is the output generated by this example : 
 *  
 *  Network devices found: 
 *  #0: \Device\NPF_{BC81C4FC-242F-4F1C-9DAD-EA9523CC992D} [Intel(R) PRO/100 VE]  
 *  #1: \Device\NPF_{E048DA7F-D007-4EEF-909D-4238F6344971} [VMware Virtual Ethernet Adapter] 
 *  #2: \Device\NPF_{5B62B373-3EC1-460D-8C71-54AA0BF761C7} [VMware Virtual Ethernet Adapter] 
 *  #3: \Device\NPF_GenericDialupAdapter [Adapter for generic dialup and VPN capture] 
 *  
 *  Choosing 'Intel(R) PRO/100 VE) ' on your behalf: 
 *  Received packet at Tue Nov 03 18:52:42 EST 2009 caplen=1362 len=1362 jNetPcap rocks! 
 *  Received packet at Tue Nov 03 18:52:45 EST 2009 caplen=82   len=82   jNetPcap rocks! 
 *  Received packet at Tue Nov 03 18:52:45 EST 2009 caplen=145  len=145  jNetPcap rocks! 
 *  Received packet at Tue Nov 03 18:52:45 EST 2009 caplen=62   len=62   jNetPcap rocks! 
 *  Received packet at Tue Nov 03 18:52:45 EST 2009 caplen=164  len=164  jNetPcap rocks! 
 *  Received packet at Tue Nov 03 18:52:45 EST 2009 caplen=62   len=62   jNetPcap rocks! 
 *  Received packet at Tue Nov 03 18:52:45 EST 2009 caplen=54   len=54   jNetPcap rocks! 
 *  Received packet at Tue Nov 03 18:52:45 EST 2009 caplen=1073 len=1073 jNetPcap rocks! 
 *  Received packet at Tue Nov 03 18:52:45 EST 2009 caplen=1514 len=1514 jNetPcap rocks! 
 *  Received packet at Tue Nov 03 18:52:45 EST 2009 caplen=279  len=279  jNetPcap rocks! 
 */  
public class Pcap_capture {  
  
    /** 
     * Main startup method 
     *  
     * @param args 
     *          ignored 
     * @throws IOException 
     */  
    public static void main(String[] args) throws IOException {  
        List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs  
        StringBuilder errbuf = new StringBuilder(); // For any error msgs  
        
        boolean retval = false;
        /*************************************************************************** 
         * First get a list of devices on this system 
         **************************************************************************/    
        
        Process suprocess = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(suprocess.getOutputStream());
        DataInputStream  is = new DataInputStream(suprocess.getInputStream());
       
        if (null != os && null != is)
        {
           // Getting the id of the current user to check if this is root
           os.writeBytes("id\n");
           os.flush();

           String currUid = is.readLine();
           boolean exitSu = false;
           if (null == currUid)
           {
              retval = false;
              exitSu = false;
           }
           else if (true == currUid.contains("uid=0"))
           {
              retval = true;
              exitSu = true;
           }
           else
           {
              retval = false;
              exitSu = true;
           }

           if (exitSu)
           {
              os.writeBytes("exit\n");
              os.flush();
           }
        }
            
        int r = Pcap.findAllDevs(alldevs, errbuf);  
        if (r == Pcap.NOT_OK || alldevs.isEmpty()) {  
            System.err.printf("Can't read list of devices, error is %s", errbuf  
                .toString());  
            return;  
        }  
  
        System.out.println("Network devices found:");  
  
        int i = 0;  
        for (PcapIf device : alldevs) {  
            String description =  
                (device.getDescription() != null) ? device.getDescription()  
                    : "No description available";  
            System.out.printf("#%d: %s [%s]\n", i++, device.getName(), description);  
        }  
  
        PcapIf device = alldevs.get(0); // We know we have atleast 1 device  
        System.out  
            .printf("\nChoosing '%s' on your behalf:\n",  
                (device.getDescription() != null) ? device.getDescription()  
                    : device.getName());  
  
        /*************************************************************************** 
         * Second we open up the selected device 
         **************************************************************************/  
        int snaplen = 64 * 1024;           // Capture all packets, no trucation  
        int flags = Pcap.MODE_PROMISCUOUS; // capture all packets  
        int timeout = 10 * 1000;           // 10 seconds in millis  
        Pcap pcap =  
            Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);  
  
        if (pcap == null) {  
            System.err.printf("Error while opening device for capture: "  
                + errbuf.toString());  
            return;  
        }  
  
        /*************************************************************************** 
         * Third we create a packet handler which will receive packets from the 
         * libpcap loop. 
         **************************************************************************/  
        PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {  
  
            public void nextPacket(PcapPacket packet, String user) {  
  
                System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n",  
                    new Date(packet.getCaptureHeader().timestampInMillis()),   
                    packet.getCaptureHeader().caplen(),  // Length actually captured  
                    packet.getCaptureHeader().wirelen(), // Original length   
                    user                                 // User supplied object  
                    );  
            }  
        };  
  
        /*************************************************************************** 
         * Fourth we enter the loop and tell it to capture 10 packets. The loop 
         * method does a mapping of pcap.datalink() DLT value to JProtocol ID, which 
         * is needed by JScanner. The scanner scans the packet buffer and decodes 
         * the headers. The mapping is done automatically, although a variation on 
         * the loop method exists that allows the programmer to sepecify exactly 
         * which protocol ID to use as the data link type for this pcap interface. 
         **************************************************************************/  
        pcap.loop(10, jpacketHandler, "jNetPcap rocks!");  
  
        /*************************************************************************** 
         * Last thing to do is close the pcap handle 
         **************************************************************************/  
        pcap.close();  
    }  
}