B4J Question B4JPackager11 issue

keirS

Well-Known Member
Licensed User
Longtime User
I get this error when trying to run a built Exe

B4X:
 class anywheresoftware.b4j.object.JavaObject (in module b4j) cannot access class sun.awt.SunToolkit (in module java.desktop) because module java.desktop does not export sun.awt 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/anywheresoftware.b4j.object.JavaObject.RunMethodJO(Unknown Source)
        at b4j/uk.sensible.mtdvat.hmrcheaders._screeninfo(Unknown Source)
        at b4j/uk.sensible.mtdvat.hmrcheaders$ResumableSub_RefreshHeaders.resume(Unknown Source)
        at b4j/anywheresoftware.b4a.BA.checkAndRunWaitForEvent(Unknown Source)
        at b4j/anywheresoftware.b4a.BA.raiseEvent2(Unknown Source)
        at b4j/anywheresoftware.b4a.BA.raiseEvent(Unknown Source)
        at b4j/anywheresoftware.b4a.keywords.Common$3.run(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(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)


I have
B4X:
 IncludedModules: ["java.desktop"],

in my JSON configuration file.

ETA:

The method called:
B4X:
Private Sub ScreenInfo As String
    Dim ScreenDef As String = ""
    Dim ScreenWidth As String = ""
    Dim ScreenHeight As String = ""
    Dim ScreenColorDepth As String  = ""
    Dim JOToolKit As JavaObject
    Dim JOGraphicsEnvironment As JavaObject
   
    JOGraphicsEnvironment.InitializeStatic("java.awt.GraphicsEnvironment")
    JOGraphicsEnvironment = JOGraphicsEnvironment.RunMethodJO("getLocalGraphicsEnvironment",Null)
    JOToolKit.InitializeStatic("java.awt.Toolkit")
    JOToolKit = JOToolKit.RunMethodJO("getDefaultToolkit",Null)
    ScreenWidth = SU.EncodeUrl("width","US-ASCII") & "="  & SU.EncodeUrl(APSU.SubstringBefore(JOToolKit.RunMethodJO("getScreenSize",Null).RunMethod("getWidth",Null),"."),"US-ASCII")
    ScreenHeight= "&" & SU.EncodeUrl("height","US-ASCII") & "="  &  SU.EncodeUrl(APSU.SubstringBefore(JOToolKit.RunMethodJO("getScreenSize",Null).RunMethod("getHeight",Null),"."),"US-ASCII")
    ScreenColorDepth = "&" & SU.EncodeUrl("colour-depth","US-ASCII") & "=" & SU.EncodeUrl(JOGraphicsEnvironment.RunMethodJO("getDefaultScreenDevice",Null).RunMethodJO("getDisplayMode",Null).RunMethod("getBitDepth",Null),"US-ASCII")
   
    ScreenDef = ScreenWidth & ScreenHeight & ScreenColorDepth
    Return ScreenDef
End Sub
 
Last edited:

keirS

Well-Known Member
Licensed User
Longtime User
Thanks for that Erel. I have found another issue. JPOI crashes when initialized when run as an .exe

B4X:
XML-BEANS compiled schema: Could not locate compiled schema resource schemaorg_apache_xmlbeans/system/sD023D6490046BA0250A839A9AD24C443/index.xsb (schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.index) - code 0
org.apache.xmlbeans.SchemaTypeLoaderException: XML-BEANS compiled schema: Could not locate compiled schema resource schemaorg_apache_xmlbeans/system/sD023D6490046BA0250A839A9AD24C443/index.xsb (schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.index) - code 0
    at b4j/org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader.<init>(Unknown Source)
    at b4j/org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.initFromHeader(Unknown Source)
    at b4j/org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl.<init>(Unknown Source)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
    at b4j/schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder.loadTypeSystem(Unknown Source)
    at b4j/schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder.<clinit>(Unknown Source)
    at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized0(Native Method)
    at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized(Unknown Source)
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(Unknown Source)
    at java.base/jdk.internal.reflect.ReflectionFactory.newFieldAccessor(Unknown Source)
    at java.base/java.lang.reflect.Field.acquireFieldAccessor(Unknown Source)
    at java.base/java.lang.reflect.Field.getFieldAccessor(Unknown Source)
    at java.base/java.lang.reflect.Field.get(Unknown Source)
    at b4j/org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(Unknown Source)
    at b4j/org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument.<clinit>(Unknown Source)
    at b4j/org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source)
    at b4j/org.apache.poi.xssf.model.ThemesTable.<init>(Unknown Source)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
    at b4j/org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(Unknown Source)
    at b4j/org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(Unknown Source)
    at b4j/org.apache.poi.ooxml.POIXMLDocumentPart.read(Unknown Source)
    at b4j/org.apache.poi.ooxml.POIXMLDocument.load(Unknown Source)
    at b4j/org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(Unknown Source)
    at b4j/org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(Unknown Source)
    at b4j/org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(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/org.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(Unknown Source)
    at b4j/org.apache.poi.ss.usermodel.WorkbookFactory.createXSSFWorkbook(Unknown Source)
    at b4j/org.apache.poi.ss.usermodel.WorkbookFactory.create(Unknown Source)
    at b4j/anywheresoftware.b4j.objects.PoiWorkbookWrapper.InitializeExisting(Unknown Source)
    at b4j/b4j.example.main._btnexcel_click(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$1.run(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(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)



B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Excel As PoiWorkbook
    Private btnExcel As Button
    Dim ExcelFile As FileChooser
End Sub
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("main")
    MainForm.Show
    ExcelFile.Initialize
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
Sub btnExcel_Click
    Dim ExcelFilename As String
    ExcelFilename = ExcelFile.ShowOpen(MainForm)
    If ExcelFilename.Length > 0 Then
        Excel.InitializeExisting(File.GetFileParent(ExcelFilename),File.GetName(ExcelFilename),"") '<---- Crashes
      
    End If
End Sub

I have attached the test project.
 

Attachments

  • jpoitest.zip
    1.9 KB · Views: 292
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
I am confused where would I add this array?

B4X:
 Excel.InitializeExisting(File.GetFileParent(ExcelFilename),File.GetName(ExcelFilename),"")

The final parameter is the password?
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
Did you try it with an .xls file or an xlsx file? It appears to work for .xls but not for .xlsx. I have attached the test files I am using.
 

Attachments

  • testfiles.zip
    11.2 KB · Views: 287
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:

1.
B4X:
Private IncludedModules As List = Array("jdk.charsets")

2. Add in line 203:
B4X:
sb.Append("opens schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443;")
The next line should be:
B4X:
sb.Append("}")

It works here tested with an unreleased version of jPOI. It is possible that more packages will need to be open like this. You need to test it.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
Thanks Erel that works. I will go through testing the application tomorrow. My concern is that there may be situations where the packages are dynamically opened based on contents of the .xlsx file.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
I have found no further problems with this. Tested several worksheets with formulas on etc and it's hasn't crashed. I have not tested wring back to a sheet as the application imports data from Excel but does not save or export to Excel..
 
Upvote 0
Top