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