B4J Question [SOLVED] Method addShutdownHook not found

Gandalf

Member
Licensed User
Longtime User
Hello,

I have Non-UI app which uses a lot of KVSs. I want to close them when the app closes. I tried to use Erel's method described here. But it throws the following error during compilation:
B4X:
main._appstart (java line: 44)
java.lang.RuntimeException: Method: addShutdownHook not found in: b4j.example.main
    at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:363)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:120)
    at b4j.example.main._appstart(main.java:44)
    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.BA.raiseEvent(BA.java:98)
    at b4j.example.main.main(main.java:28)
Tested with Java 8, 11, 14 - result is the same. What I'm doing wrong?
 
Solution

teddybear

Well-Known Member
Licensed User
You are missing this code
B4X:
#if Java
public static void addShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
               ba.raiseEventFromDifferentThread(null, null, 0, "shutdown_hook", true, null);
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
}
#End If
 
Upvote 1
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…