B4J Question Help request: call a (simple) Java class from B4J

FabioAlbaneseTv

Member
Licensed User
Hello,

sorry, I didn't find any answer searching on the forum till now.
I wish to try to call a Java class/function from B4J (not B4A) and I've tried this simple example:

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private NativeMe As JavaObject
End Sub

Sub AppStart (Args() As String)
    NativeMe.InitializeContext
    Dim s As String = NativeMe.RunMethod("FirstMethod", Null)
    Log(s)
End Sub


#If JAVA
public String FirstMethod() {
   return "Hello World!";
}
#End if

I've included the JavaObjext library, but I continue to get this error when I start the application.

B4X:
Waiting for debugger to connect...
Program started.
Error occurred on line: 12 (Main)
java.lang.NoSuchFieldException: sharedProcessBA
    at java.base/java.lang.Class.getDeclaredField(Class.java:2412)
    at anywheresoftware.b4j.object.JavaObject.InitializeContext(JavaObject.java:57)
    at b4j.example.main._appstart(main.java:61)
    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:566)
    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:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at b4j.example.main.main(main.java:29)
Program terminated (StartMessageLoop was not called).


I don't understand what I missed.
Can someone help me ?
Thank you.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
but I continue to get error when I start the application
do we need to guess the error?

Please post the full error to get better chances to get help.
 
Upvote 0

FabioAlbaneseTv

Member
Licensed User
Ok, sorry.

This is what I get:


B4X:
Waiting for debugger to connect...
Program started.
Error occurred on line: 12 (Main)
java.lang.NoSuchFieldException: sharedProcessBA
    at java.base/java.lang.Class.getDeclaredField(Class.java:2412)
    at anywheresoftware.b4j.object.JavaObject.InitializeContext(JavaObject.java:57)
    at b4j.example.main._appstart(main.java:61)
    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:566)
    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:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at b4j.example.main.main(main.java:29)
Program terminated (StartMessageLoop was not called).
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
NativeMe.InitializeContext = B4A only Method.

B4X:
Sub Process_Globals
    Private NativeMe As JavaObject
End Sub

Sub AppStart (Args() As String)
    NativeMe = Me
    Dim s As String = NativeMe.RunMethod("FirstMethod", Null)
    Log(s)
End Sub


#If JAVA
public static String FirstMethod() {
   return "Hello World!";
}
#End if
 
Upvote 2

FabioAlbaneseTv

Member
Licensed User
NativeMe.InitializeContext = B4A only Method.

B4X:
Sub Process_Globals
    Private NativeMe As JavaObject
End Sub

Sub AppStart (Args() As String)
    NativeMe = Me
    Dim s As String = NativeMe.RunMethod("FirstMethod", Null)
    Log(s)
End Sub


#If JAVA
public static String FirstMethod() {
   return "Hello World!";
}
#End if

Thank you a lot !!!!!!!!!!!
 
Upvote 0
Top