B4J Question setfield2 has error in release mode not in debug mode

HI Erel
I have a problem with Reflector
please tell me the solution
my code is this:

Dim PC As Printer = GetPrinter(myLabelPrinter)
Log("PC") : Log(PC)

Dim myResult As Boolean = False
For Each SP As JavaObject In PC.GetPrinterAttributes.GetSupportedPapers
Dim PP As Paper : PP.Initialize
PP=Paper_static.C
PP.SetObject(SP)
Log("PP") : Log(PP)
Try
Dim VAL As String=PP.tostring
Log("VAL") : Log(VAL)
If VAL.contains("2 x 4")=True Then
Log("PP2") : Log(PP) : Log(PP.GetWidth) : Log(PP.GetHeight)
Dim r As Reflector
r.Target = SP
Log("GetField")
r.setField2("width", 60)
r.SetField2("height", 30)
PP.SetObject(SP)

myResult = True
Exit
End If
Catch
Log(LastException)
End Try
Next

but I got java error on SetField2 in release mode not in debug mode

java.lang.reflect.InaccessibleObjectException: Unable to make field private double javafx.print.Paper.width accessible: module javafx.graphics does not "opens javafx.print" to unnamed module @537c850b

Best Regards
HosseinEtezadi
 

stevel05

Expert
Licensed User
Longtime User
What are you trying to do?

If the page size you want is not available in GetSupportedPapers, it is unlikely that the printer will respond to the paper size being changed, even if you manage to change it.

The easiest way to use an unavailable paper size would be to choose a larger size, then restrict your layout to the size you actually want to print.
 
Upvote 0
B4X:
Dim PC As Printer = GetPrinter(myLabelPrinter)
   Log("PC") : Log(PC)
  
   Dim myResult As Boolean = False
   For Each SP As JavaObject  In PC.GetPrinterAttributes.GetSupportedPapers
       Dim PP As Paper : PP.Initialize
       PP=Paper_static.C
       PP.SetObject(SP)
       Log("PP") : Log(PP)
       Try            
           Dim VAL As String=PP.tostring
           Log("VAL") : Log(VAL)
           If VAL.contains("2 x 4")=True  Then
               Log("PP2") : Log(PP) : Log(PP.GetWidth)  : Log(PP.GetHeight)
               Dim r As Reflector
               r.Target = SP
               Log("GetField")
               r.setField2("width", 60)
               r.SetField2("height", 30)
               PP.SetObject(SP)
          
               myResult = True
               Exit
           End If
       Catch
           Log(LastException)
       End Try
   Next
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Check your logs, you should be seeing an entry:

B4X:
(NoSuchFieldException) java.lang.NoSuchFieldException: width

The paper class does not have a field for width and height. It is not configurable.

Better still remove the Try Catch block until the code is working.
 
Upvote 0
Thanks Steve
I want to print barcode on label size 60*30 mm
print is ok in Debug mode but in release mode I got java error
 
Upvote 0
Thanks Steve
I removed the Try Catch but code just works ok in debug mode
but in release mode I get Java error:
java.lang.reflect.InaccessibleObjectException: Unable to make field private double javafx.print.Paper.width accessible: module javafx.graphics does not "opens javafx.print" to unnamed module @59456f4e
at java.base/java.lang.reflect.Field.setAccessible(Field.java:170)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:176)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:280)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Which version of JavaFX are you using?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You will need to add something like:
B4X:
#VirtualMachineArgs: --add-opens javafx.graphics/javafx.print=ALL-UNNAMED
To the top of you main Module.
And
B4X:
#PackagerProperty : VMArgs = --add-opens  javafx.graphics/javafx.print=b4j
If you want to build a package.

I can't test it as I don't get the same errors, which is why I am wondering which version you are using.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This is a better way of creating a Custom Paper rather than hacking an existing instance:

B4X:
    Dim Units As JavaObject
    Units.InitializeStatic("com.sun.javafx.print.Units")
    Dim PrintHelper As JavaObject
    PrintHelper.InitializeStatic("com.sun.javafx.print.PrintHelper")
    Dim Paper As JavaObject = PrintHelper.RunMethod("createPaper",Array("60mm x 30mm",60.0,30.0,Units.GetField("MM")))
    Log(Paper)

You will still need the --add-opens directives above plus:
B4X:
#PackagerProperty : VMArgs = --add-opens  javafx.graphics/com.sun.javafx.print=b4j
To package the project.
 
Last edited:
Upvote 0
thanks a lot, Steve
that problem solved
Best Regards
 
Upvote 0
thanks, Steve
I used this code but I got this java error:
java.lang.IllegalAccessException: class anywheresoftware.b4j.object.JavaObject cannot access class com.sun.javafx.print.Units (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.print to unnamed module @e05be4e
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have you added the VirtualMachineArg directive in post #9 ?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Looks like you may also need:
B4X:
#VirtualMachineArgs: --add-opens javafx.graphics/com.sun.javafx.print=ALL-UNNAMED

i didn't need it, I guess it depends on which java version you are using.
 
Last edited:
Upvote 0
Hi Steve
I could not use this code instead I had to use "SetField2" from jreflection library to hack the paper size.
Best Regards
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…