B4J Question [SOLVED] Error Running Stand Alone App IllegalAccessException com.sun.proxy.jdk.proxy2.$Proxy3

MrKim

Well-Known Member
Licensed User
Longtime User
The full message is below.

B4X:
java.lang.IllegalAccessException: class anywheresoftware.b4j.object.JavaObject (in module b4j) cannot access class com.sun.proxy.jdk.proxy2.$Proxy3 (in module jdk.proxy2) because module jdk.proxy2 does not export com.sun.proxy.jdk.proxy2 to module b4j
        at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Unknown Source)
        at java.base/java.lang.reflect.AccessibleObject.checkAccess(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at b4j/anywheresoftware.b4j.object.JavaObject.RunMethod(Unknown Source)
        at b4j/com.stevel05.draganddrop.dadmod._setforegroundwindow(Unknown Source)
        at b4j/com.stevel05.draganddrop.dadmod._openjobinsk2010(Unknown Source)
        at b4j/com.stevel05.draganddrop.main._jobnumlbl_mouseclicked(Unknown Source)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at b4j/anywheresoftware.b4a.BA.raiseEvent2(Unknown Source)
        at b4j/anywheresoftware.b4a.BA.raiseEvent(Unknown Source)
        at b4j/anywheresoftware.b4j.objects.NodeWrapper$1.handle(Unknown Source)
        at b4j/anywheresoftware.b4j.objects.NodeWrapper$1.handle(Unknown Source)
        at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
        at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
        at javafx.base/javafx.event.Event.fireEvent(Unknown Source)
        at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Unknown Source)
        at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8200(Unknown Source)
        at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Unknown Source)
        at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Unknown Source)
        at javafx.graphics/javafx.scene.Scene.processMouseEvent(Unknown Source)
        at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
        at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.View.notifyMouse(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)

Here is the code (I am aware it has other issues ? ):
B4X:
Sub SetForegroundWindow(Title As String, JustFind As Boolean) As Boolean
Try
 
Dim WU As JavaObject
WU.InitializeStatic("com.sun.jna.platform.WindowUtils")
Dim user32 As JavaObject
user32 = user32.InitializeStatic("com.sun.jna.platform.win32.User32").GetField("INSTANCE")
Dim L As List = WU.RunMethod("getAllWindows",Array(False))
For Each JO As JavaObject In L
    Dim t As String = JO.RunMethod("getTitle",Null)
    If t.EqualsIgnoreCase(Title) Then
        If JustFind Then
            Return True
        Else
            Dim hwnd As Object = JO.RunMethod("getHWND", Null)
            user32.RunMethod("SetForegroundWindow", Array(hwnd))
            user32.RunMethod("SetFocus", Array(hwnd))
            user32.RunMethod("ShowWindow", Array(hwnd, 9))'SW_RESTORE
            Return False
        End If
    End If
Next
Return False
Log("Window not found")
Catch
    Main.Msgbox.Show(LastException, "SetForegroundWindow")
End Try
End Sub
It is a slightly modified version of Erel's code Here

I am guessing it has to do with needing to add
B4X:
    #AdditionalJar: jna-5.0.0
    #AdditionalJar: jna-platform-5.0.0
using #PackagerProperty:
or #CustomBuildAction:

But I am not sure how.

I tried
B4X:
    #PackagerProperty: IncludedModules = jna-5.0.0
    #PackagerProperty: IncludedModules = jna-platform-5.0.0
but it did not work. Gave this error:

B4X:
B4JPackager11 Version 1.40
.
.
.
Explicitly excluded modules: [javafx.web]
Included modules: [jna-5.0.0, jna-platform-5.0.0, java.base, java.datatransfer, java.desktop, java.logging, java.naming, java.security.jgss, java.sql, java.transaction.xa, java.xml, javafx.base, javafx.controls, javafx.fxml, javafx.graphics, javafx.media, javafx.swing, java.scripting, jdk.unsupported, jdk.unsupported.desktop, jdk.jsobject, jdk.xml.dom]
Running: C:\Java\jdk-11.0.1\bin\javac
.
module-info.java:2: error: ';' expected
requires jna-5.0.0;
            ^
1 error
It runs fine in Debug mode.
Any help greatly appreciated.
 

Attachments

  • 1654759562988.png
    17.6 KB · Views: 333
Last edited:
Solution
I see it too. It is related to JNA using a special dynamic proxy that can no longer be accessed with reflection when the app is modularized.

You will need to use inline Java instead:
B4X:
Sub SetForegroundWindow(Title As String, JustFind As Boolean) As Boolean
    Return Me.As(JavaObject).RunMethod("SetForegroundWindow", Array(Title, JustFind))
End Sub

#if Java
import com.sun.jna.platform.win32.*;
public static boolean SetForegroundWindow(String title, boolean justFind) {
    User32 user32 = com.sun.jna.platform.win32.User32.INSTANCE;
    for (com.sun.jna.platform.DesktopWindow s : com.sun.jna.platform.WindowUtils.getAllWindows(false)) {
        String t = s.getTitle();
        if (t.toLowerCase().equals(title.toLowerCase())) {...
Cookies are required to use this site. You must accept them to continue using the site. Learn more…