B4J Question Problem with openjdk 19.x

kpmais

Member
Licensed User
Longtime User
Hi everyone.
To convert centimeters to pixels, I use the following routine:

B4X:
Private Sub GetPixelProCm As Int

    Dim jo As JavaObject
    jo.InitializeStatic("java.awt.Toolkit")
    jo = jo.RunMethod("getDefaultToolkit",Null)
    Return Floor(jo.RunMethod("getScreenResolution",Null)/2.18)

End Sub

This works fine with OpenJDK 14.x.
However, with OpenJDK 19.x, I get the following error when running:
java.lang.IllegalAccessException: class anywheresoftware.b4j.object.JavaObject cannot access class sun.awt.windows.WToolkit (in module java.desktop) because module java.desktop does not export sun.awt.windows to unnamed module @6224ccc0

at

B4X:
 jo.RunMethod("getScreenResolution",Null)

I have not found any indication that the awt.toolkit has changed in openjdk 19.x.

Can anyone help me?
Many thanks to the readers.
 

Daestrum

Expert
Licensed User
Longtime User
Add (Tested in Java 23)
B4X:
    #VirtualMachineArgs:  --add-exports java.desktop/sun.awt.windows=ALL-UNNAMED
 
Last edited:
Upvote 1

kpmais

Member
Licensed User
Longtime User
C'est formidable !!!
Thank you very much. It works!!! :)

I'll have to get into this topic.
Thanks again.
 
Upvote 0

kpmais

Member
Licensed User
Longtime User
Sorry, the joy is only partial.
It runs fine in debug and release.
However, when I produce a standalone app, the error occurs again. I'm using OpenJDK 19.x here as well.
I'll try a higher Java version.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
For the packager you will need to add:
B4X:
#PackagerProperty: VMArgs = --add-exports java.desktop/sun.awt.windows=b4j
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
just add the similar command to the packager too (Steve beat me to it)
 
Upvote 0

kpmais

Member
Licensed User
Longtime User
Thanks again to both of you.
It works now :).

But why is the assignment "=b4j" and not "=ALL-UNNAMED"?
I'm currently puzzling over that.
 
Upvote 0
Top