JNI Adventures

Posted by & filed under Uncategorized.

So, we have this issue where we can’t recreate test scenarios because of changing data on the back-end. A collegue of mine and I were discussing the issue when I began to think that we could use a packet sniffer to passively record network traffic and then play back those packets at anytime, thereby simulating any type of network request, kind of like a tape recorder faithfully replays whatever was recorded on the tape. That night I ‘googled’ packet sniffer and came across winpcap. So, for the past few nights I’ve been wrapping the winpcap C library in Java, only to find out tonight that jNetPCAP beat me to the punch. Not all was lost because I was able to sharpen my JNI skills. You find all the code here. PCAP JNI CODE

I started by writing the Java abstractpacketsniffer.java. Defined my native methods.

public static native NetworkInterface[] findAllDevices();
private native void startCapture(String deviceName);

And added the code to load the native library.


I then ran javah to produce my C header file. Which produced this. AbstractPacketSniffer.h

>javah -jni com.wcgllc.net.pcap.AbstractPacketSniffer

Wrote the C code and then compiled it with cygwin gcc using the following command.

>gcc -Wall -mno-cygwin \
-I/cygdrive/c/Program\ Files/Java/jdk1.5.0_05/include \
-I/cygdrive/c/Program\ Files/Java/jdk1.5.0_05/include/win32 \
-I/cygdrive/c/projects/winpcap/WpdPack/Include \
-L/cygdrive/c/projects/winpcap/WpdPack/Lib \
-shared com_wcgllc_net_pcap_AbstractPacketSniffer.c \
-Wl,–add-stdcall-alias -o ../classes/packetsniffer.dll -lwpcap

You must be logged in to post a comment. Log in