B4J Question more than one sk lan in pc

R. Romano

Member
Licensed User
I did it, but it didn't work in B4J.

- I think the problem is on jo.InitializeContext becouse it is only for b4a

Below my cronolog

- I used B4j (v. 7.31)

- Add Lib JavaObject (v.2.06)


- The code is:

Sub AppStart (Form1 As Form, Args() As String)

Log("************* Start program ************" )
Log ($"Device Time is $Time{ DateTime.Now}"$ )
Log("*****************************************" )

MainForm = Form1
MainForm.RootPane.LoadLayout("1") 'Load the layout file.
MainForm.Show

Dim jo As JavaObject
jo.InitializeContext
Log(jo.RunMethod("getIp", Array("wlan0"))) 'change to eth0 to get the ethernet address

End Sub

#if JAVA
import java.net.InetAddress;
import java.net.Inet6Address;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

public static String getIp(String interfaceName) throws Exception {
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
NetworkInterface ni = e.nextElement();
if (ni.getName().equals(interfaceName)) {
Enumeration<InetAddress> en2 = ni.getInetAddresses();
while (en2.hasMoreElements()) {
InetAddress ia = en2.nextElement();
if (!ia.isLoopbackAddress()) {
if ((ia instanceof Inet6Address) == false) {
return ia.getHostAddress();
}
}
}
}
}
return "";
}

#End If

- The log is:

************* Start program ************
Device Time is 09:31:28
*****************************************
main._appstart (java line: 118)
java.lang.NoSuchFieldException: sharedProcessBA
at java.lang.Class.getDeclaredField(Class.java:2070)
at anywheresoftware.b4j.object.JavaObject.InitializeContext(JavaObject.java:57)
at b4j.example.main._appstart(main.java:118)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at b4j.example.main.start(main.java:43)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)


Thanks
 
Upvote 0

R. Romano

Member
Licensed User
I change a little bit the Java code.

I hope this can be userfully to someone.

- B4J Code

Sub Example_Test
Dim jo As JavaObject=Me
Dim AllInterfaces As List
AllInterfaces.Initialize
AllInterfaces=jo.RunMethod("getAllInterfaces", Null)

For i =0 To AllInterfaces.Size -1
Log (AllInterfaces.Get(i))
Next

Log(jo.RunMethod("getIp", Array("wlan0"))) 'change to eth0 to get the ethernet address

End Sub


- Java Code

import java.net.InetAddress;
import java.net.Inet6Address;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

import java.util.ArrayList;
import java.util.List;


public static String getIp(String interfaceName) throws Exception {
//BA.Log("Search " + interfaceName);
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) {
NetworkInterface ni = e.nextElement();
if (ni.getName().equals(interfaceName)) {
//BA.Log("Found Interface ...");
Enumeration<InetAddress> en2 = ni.getInetAddresses();
while (en2.hasMoreElements()) {
InetAddress ia = en2.nextElement();

if (!ia.isLoopbackAddress()) {
if ((ia instanceof Inet6Address) == false) {
return ia.getHostAddress();
}
}
}
}
}
//BA.Log("NOT Found IPs");
return "";

}


public static List<String> getAllInterfaces() throws Exception {
//BA.Log("Search");
List<String> mydata = new ArrayList<>();
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements(); ) {
mydata.add("" + e.nextElement());
}
return (mydata);
}


#End If
 
Last edited:
Upvote 0

R. Romano

Member
Licensed User
This is the same code, but so it is better.


- B4J Code

B4X:
Sub Example_Test
     Dim jo As JavaObject=Me
     Dim AllInterfaces As List
     AllInterfaces.Initialize 
     AllInterfaces=jo.RunMethod("getAllInterfaces", Null)

     For i =0 To AllInterfaces.Size -1
          Log (AllInterfaces.Get(i))
     Next

    Log(jo.RunMethod("getIp", Array("wlan0"))) 'change to eth0 to get the ethernet address

End Sub

- Java Code
B4X:
#if JAVA

import java.net.InetAddress;
import java.net.Inet6Address;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;

import java.util.ArrayList;
import java.util.List;


public static String getIp(String interfaceName) throws Exception {
  //BA.Log("Search " + interfaceName);
  for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
     NetworkInterface ni = e.nextElement();
     if (ni.getName().equals(interfaceName)) {
       //BA.Log("Found Interface ...");
       Enumeration<InetAddress> en2 = ni.getInetAddresses();
       while (en2.hasMoreElements()) {
         InetAddress ia = en2.nextElement();
                 
         if (!ia.isLoopbackAddress()) {
           if ((ia instanceof Inet6Address) == false) {
             return ia.getHostAddress();
           }
         }
       }
     }
  }
      //BA.Log("NOT Found IPs");
    return "";
 
  }


 public static List<String> getAllInterfaces() throws Exception {
  //BA.Log("Search");
  List<String> mydata = new ArrayList<>();
  for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
    mydata.add("" + e.nextElement());
  }
  return (mydata);
  }


#End If

Regards
 
Upvote 0
Top