B4J Question pcap4j usage in b4j, help needed

madru

Active Member
Licensed User
Longtime User
Hello friends,

who has knowledge with pcap4j, and that in b4j

I am trying to send generated wifi packets natively, but I am failing with inline java, always get java.lang.ClassNotFoundException: java$lang$RawFrameSender


which I have done so far:


B4X:
#AdditionalJar: pcap4j-core-1.8.2 ' pcap4j-core
#AdditionalJar: pcap4j-packetfactory-static-1.8.2 ' pcap4j-packetfactory-static


B4X:
Sub SendmRawFrame(mframe() As Byte, ifaceName As String)

    Dim inline As JavaObject
    inline = Me
    'inline.InitializeStatic("RawFrameSender")
    inline.InitializeNewInstance("RawFrameSender",Null)
    Try
        inline.RunMethod("sendRawFrame", Array(ifaceName, mframe))
        Log("Frame sent successfully")
    Catch
        Log("Error sending frame: " & LastException.Message)
    End Try
End Sub

Private Sub Button2_Click
'================= Packet example =========================
'00 00 08 00 00 00 00 00 80 00 00 00 FF FF FF FF  | ................
'FF FF 01 02 03 04 05 06 01 02 03 04 05 06 00 00  | ................
'00 08 4A 51 4B 32 58 42 35 55 01 04 01 05 07 09  | ..JQK2XB5U......
'03 01 0B DD 52 26 37 12 58 62 13 10 01 EE 00 DF  | ....R&7.Xb......
'0F 57 32 31 35 36 4F 4E 4D 4B 35 56 52 59 32 53  | .W2156ONMK5VRY2S
'49 2A 05 DB FE DB FE 5D 00 0B 00 16 00 3E 00 B4  | I*.....].....>..
'00 93 00 B0 00 16 00 E1 00 36 73 00 FF 09 F8 5D  | .........6s....]
'00 01 12 38 49 59 56 55 4C 49 33 44 48 36 52 48  | ...8IYVULI3DH6RH
'34 52 35 44 34 00 00                             | 4R5D4..
'==========================================

    Dim frame() As Byte = BuildWirelessFrame() ' the generated frame
    Dim ifaceName As String = "wlan0"

    ' Send the raw frame
    SendmRawFrame(frame, ifaceName)
End Sub



Java:
#If Java
import org.pcap4j.core.*;
import org.pcap4j.packet.Packet;
import org.pcap4j.packet.UnknownPacket;

public class RawFrameSender {

    // Static method for sending raw frames
    public  void sendRawFrame(String ifaceName, byte[] frameBytes) throws PcapNativeException, NotOpenException {
        PcapNetworkInterface nif = Pcaps.getDevByName(ifaceName);
        if (nif == null) {
            throw new IllegalArgumentException("Network interface not found: " + ifaceName);
        }

        // Open the network interface for sending raw frames
        PcapHandle handle = nif.openLive(65536, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, 10);
        try {
            // Create and send the packet
            Packet packet = UnknownPacket.newPacket(frameBytes, 0, frameBytes.length);
            handle.sendPacket(packet);
        } finally {
            // Ensure the handle is closed to free resources
            handle.close();
        }
    }
}
#End If


java.lang.ClassNotFoundException: java$lang$RawFrameSender
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:289)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:84)
at b4j.example.main._sendmrawframe(main.java:899)
at b4j.example.main._button2_click(main.java:879)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
at anywheresoftware.b4a.BA$1.run(BA.java:236)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:832)



also tried to compile RawFrameSender into a .jar File without success :(
javac -cp path/pcap4j-core-1.8.2.jar:ath/pcap4j-packetfactory-static-1.8.2.jar path/RawFrameSender.java
jar cf RawFrameSender.jar RawFrameSender.class

Java:
public static void sendRawFrame(String ifaceName, byte[] frameBytes) throws PcapNativeException, NotOpenException {
    // Get the network interface by name
    PcapNetworkInterface nif = Pcaps.getDevByName(ifaceName);
    if (nif == null) {
        throw new IllegalArgumentException("Network interface not found: " + ifaceName);
    }

    // Open a handle for sending packets
    PcapHandle handle = nif.openLive(65536, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, 10);

    // Create a raw packet
    Packet packet = UnknownPacket.newPacket(frameBytes, 0, frameBytes.length);

    // Send the packet
    handle.sendPacket(packet);

    // Close the handle
    handle.close();
}

with this inline code
java3:
import org.pcap4j.core.*;
import org.pcap4j.packet.Packet;
import org.pcap4j.packet.UnknownPacket;

public class RawFrameSender {

    public static void sendRawFrame(String ifaceName, byte[] frameBytes) throws PcapNativeException, NotOpenException {
        // Get interface by name
        PcapNetworkInterface nif = Pcaps.getDevByName(ifaceName); // class org.pcap4j.core.Pcaps
        if (nif == null) {
            throw new IllegalArgumentException("Network interface not found: " + ifaceName);
        }

        // Open
        PcapHandle handle = nif.openLive(65536, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, 10);

        // Create a raw packet
        Packet packet = UnknownPacket.newPacket(frameBytes, 0, frameBytes.length);

        handle.sendPacket(packet);

        // Close  handle
        handle.close();
    }
}

am thankful for any help
 

teddybear

Well-Known Member
Licensed User
B4X:
inline.InitializeNewInstance("RawFrameSender",Null)

You need to add main class name for it. I assume it is b4j.example.main

B4X:
inline.InitializeNewInstance("b4j.example.main.RawFrameSender",Array(Null))
 
Upvote 0
Top