B4J Question cannot access class sun.security.x509.X509CertImpl (in module java.base) because module java.base does not export sun.security.x509 to unnamed module

aeric

Expert
Licensed User
Longtime User
I created an example.

It works in Debug and Release but if I call the jar in command prompt, I get errors.
B4X:
C:\Users\aeric\Desktop\readp12\Objects>C:\Java\jdk-19.0.2\bin\java -jar readp12.jar MyPassword ../Objects/keystore.p12
Creating ../Objects/keystore.p12...
Success

Reading ../Objects/keystore.p12...
Alias: b4x
main._appstart (java line: 53)
java.lang.RuntimeException: java.lang.IllegalAccessException: class anywheresoftware.b4j.object.JavaObject cannot access class sun.security.x509.X509CertImpl (in module java.base) because module java.base does not export sun.security.x509 to unnamed module @5ae9a829
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:140)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
        at anywheresoftware.b4a.keywords.Common$3.run(Common.java:1118)
        at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
        at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
        at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:180)
        at b4j.example.main._appstart(main.java:53)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
        at b4j.example.main.main(main.java:28)
Caused by: java.lang.IllegalAccessException: class anywheresoftware.b4j.object.JavaObject cannot access class sun.security.x509.X509CertImpl (in module java.base) because module java.base does not export sun.security.x509 to unnamed module @5ae9a829
        at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:420)
        at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:709)
        at java.base/java.lang.reflect.Method.invoke(Method.java:569)
        at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
        at b4j.example.main._readcert(main.java:224)
        at b4j.example.main$ResumableSub_CheckCert.resume(main.java:129)
        at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:156)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:105)
        ... 11 more

C:\Users\aeric\Desktop\readp12\Objects>

If I add the extra parameters, it runs.
B4X:
C:\Users\aeric\Desktop\readp12\Objects>C:\Java\jdk-19.0.2\bin\java --add-opens java.base/sun.security.x509=ALL-UNNAMED --add-exports java.base/sun.security.x509=ALL-UNNAMED -jar readp12.jar MyPassword ../Objects/keystore.p12
Creating ../Objects/keystore.p12...
Success

Reading ../Objects/keystore.p12...
Alias: b4x
Serial Number: 13353879566025462026

C:\Users\aeric\Desktop\readp12\Objects>
 
Solution
Here is my findings.

I think it will still work if you create an executable using the built in Stand Alone Packager.
I saw there is a warning when building Standalone Package in Java 19 that it only supported JDK are 11, 14 and 16.
I don't have JDK 16 so I test with any JDK available on my PC.

With jdk-11.0.1, it is not working.

With jdk11.0.15_9 and jdk-14.0.1, it works by adding the following line:
B4X:
#PackagerProperty: VMArgs = --add-opens java.base/sun.security.x509=b4j

teddybear

Well-Known Member
Licensed User
I think #VirtualMechanicArgs is only for IDE running. you have to add "-add-exports java.base/sun.security.x509=ALL-UNNAMED" for running jar on cmdline
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I think it is similar to this question.

for running jar on cmdline
So you mean I have no choice but to add these export parameters when running with cmdline?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You might be able to put them in a simple text file and use @yourfile on the command line. (not tried it for running things - I only use on javac.exe and jar.exe))

yourfile.txt
--add-opens java.base/sun.security.x509=ALL-UNNAMED
--add-exports java.base/sun.security.x509=ALL-UNNAMED

Use like (can't remember if you need the extension .txt)
java @yourfile.txt -jar readp12.jar MyPassword ../Objects/keystore.p12
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I think it will still work if you create an executable using the built in Stand Alone Packager.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Here is my findings.

I think it will still work if you create an executable using the built in Stand Alone Packager.
I saw there is a warning when building Standalone Package in Java 19 that it only supported JDK are 11, 14 and 16.
I don't have JDK 16 so I test with any JDK available on my PC.

With jdk-11.0.1, it is not working.

With jdk11.0.15_9 and jdk-14.0.1, it works by adding the following line:
B4X:
#PackagerProperty: VMArgs = --add-opens java.base/sun.security.x509=b4j
 
Upvote 0
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…