1

所以我有两个运行良好的项目。但是当我尝试将它们放在一起时,我会立即在底部得到错误。

单独的文件(前两个属于一起): File1 :

package pkg2buttongui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author root
 */
public class TestJavaHttpsDecrypt{

    private JFrame frame;
    private JTextField input;
    private JButton startButton;
    private JButton stopButton;

    public void go(){
        final CaptureNetTraffic cnt = new CaptureNetTraffic();
        frame = new JFrame("CaptureControl");
        frame.setLayout(new GridLayout(3,1));

        input = new JTextField("enter NIC");
        startButton = new JButton("start");
        stopButton = new JButton("stop");

        startButton.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    cnt.run(input.getText());
                }
            }
        );
        stopButton.addActionListener(
            new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    cnt.stopCap();
                }
            }
        );

        frame.add(input);
        frame.add(startButton);
        frame.add(stopButton);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(800,500,100,150);
        frame.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        new TestJavaHttpsDecrypt().go();
    }

}

文件2:

package pkg2buttongui;

import javax.swing.SwingWorker;

/**
 *
 * @author root
 */
public class CaptureNetTraffic extends Thread{

    int i = 0;

    private String text = null;
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            while(true){
                System.out.println(text);
                Thread.sleep(1);
                i++;
            }
        }
    };

    public void stopCap(){
        //Thread.interrupted();
        worker.cancel(true);
        System.out.println("stopping capture at " + i);
    }

    public void run(final String var){
        text = var;
        worker.execute();
    }
}

第二个项目:

    import java.io.File;  
    import java.io.FileNotFoundException;
    import org.jnetpcap.Pcap;  
    import org.jnetpcap.PcapDumper;  


    public class PcapDumperExample {  
      public static void main(String[] args) throws FileNotFoundException {  
        StringBuilder errbuf = new StringBuilder();     // For any error msgs  

        /*************************************************************************** 
         * 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;           // 10 seconds in millis  
        Pcap pcap = Pcap.openLive("eth1", snaplen, flags, timeout, errbuf);  
        if (pcap == null) {  
          System.err.printf("Error while opening device for capture: %s\n",   
            errbuf.toString());  
          return;  
        }  

        /*************************************************************************** 
         * create a PcapDumper and associate it with the pcap capture 
         ***************************************************************************/  
        String ofile = "mycap.cap";  
        PcapDumper dumper = pcap.dumpOpen(ofile); // output file  

        /*************************************************************************** 
         * enter the loop and tell it to capture 10 packets. We pass 
         * in the dumper created in step 3 
         **************************************************************************/  
        pcap.loop(100, dumper);  

        File file = new File(ofile);  
        System.out.printf("%s file has %d bytes in it!\n", ofile, file.length());


        /*************************************************************************** 
         * Last thing to do is close the dumper and pcap handles 
         **************************************************************************/  
        dumper.close(); 
        pcap.close();  
        }  
      }  
    }  

现在在这里我试着把它们放在一起,这样我就可以控制捕捉的持续时间。为此,我将 loop.infinite 放在 pcap.loop 而不是 100 中。并将以下文件与 file1 一起使用:

package testjavahttpsdecrypt;

import java.io.File;
import javax.swing.SwingWorker;

import static org.jnetpcap.Pcap.LOOP_INFINITE;

import org.jnetpcap.Pcap;  
import org.jnetpcap.PcapDumper;

/**
 *
 * @author root
 */
public class CaptureNetTraffic extends Thread {

    int i = 0;    
    private String text = null;
    Pcap pcap = new Pcap();
    PcapDumper dumper = new PcapDumper();
    String ofile = "meins.cap";  
    StringBuilder errbuf = new StringBuilder();
    int snaplen = 64 * 1024;           // Capture all packets, no trucation  
    int flags = Pcap.MODE_PROMISCUOUS; // capture all packets  
    int timeout = 10000;           // 10 seconds in millis  


    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            while(true){
                pcap = Pcap.openLive(text, snaplen, flags, timeout, errbuf);  
                if (pcap == null) {  
                  System.err.printf("Error while opening device for capture: %s\n",   
                    errbuf.toString());  
                  return null;  
                }
                dumper = pcap.dumpOpen(ofile);

                pcap.loop(100, dumper);                  
            }
        }
    };

    public void stopCap(){
        File file = new File(ofile);  
        System.out.printf("%s file has %d bytes in it!\n", ofile, file.length());
        dumper.close();
        pcap.close();
        //Thread.interrupted();
        worker.cancel(true);
        System.out.println("stopping capture at " + i);
    }

    public void run(final String var){
        text = var;
        worker.execute();
    }
}

现在当我运行 file1 我得到这个错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: 
com.slytechs.library.NativeLibrary.dlopen(Ljava/lang/String;)J
    at com.slytechs.library.NativeLibrary.dlopen(Native Method)
    at com.slytechs.library.NativeLibrary.<init>(Unknown Source)
    at com.slytechs.library.JNILibrary.<init>(Unknown Source)
    at com.slytechs.library.JNILibrary.loadLibrary(Unknown Source)
    at com.slytechs.library.JNILibrary.register(Unknown Source)
    at com.slytechs.library.JNILibrary.register(Unknown Source)
    at com.slytechs.library.JNILibrary.register(Unknown Source)
    at org.jnetpcap.Pcap.<clinit>(Unknown Source)
    at testjavahttpsdecrypt.CaptureNetTraffic.<init>(CaptureNetTraffic.java:24)
    at testjavahttpsdecrypt.TestJavaHttpsDecrypt.go(TestJavaHttpsDecrypt.java:23)
    at testjavahttpsdecrypt.TestJavaHttpsDecrypt.main(TestJavaHttpsDecrypt.java:62)

由于第二个项目自己正确运行,我相信我已经正确设置了库(这是来自 stackoverflow 的一篇帖子的提示)我感谢每一个提示/帮助。

4

0 回答 0