Thursday 23 March 2023

javax.xml.bind::jaxb-api not working in JDK17

 When you are trying to update the JDK version 8 or 11 to JDK 17 and if you are using javax.xml.bind::jaxb-api then you will see the below error during the startup of the application

Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) throws java.lang.ClassFormatError accessible: module java.base does not \"opens java.lang\" to unnamed module @5e922278\n\tat java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)\n\tat 

the root cause is that the javax.xml.bind::jaxb-api is trying to use internal java API using reflection and the support is stopped from JDK16 due to the introduction of the "Strongly encapsulate JDK internals by default" feature.

Recommendation: The recommendation is to move away/upgrade the libraries that use reflection to load java internal classes

And the correct alternate for javax.xml.bind::jaxb-api is jakarta.xml.bind::jakarta.xml.bind-api

Or another way to resolve this is to add this java startup arguments option --add-opens=java.base/java.lang=ALL-UNNAMED

Interesting study material: https://blogs.oracle.com/javamagazine/post/a-peek-into-java-17-continuing-the-drive-to-encapsulate-the-java-runtime-internals


mvn flatten:flatten

The flatten plugin from maven helps to remove any development-specific element, build specific element or environment-specific elements in the pom file and prepares a pom file that can be published and can be consumed by other

https://www.mojohaus.org/flatten-maven-plugin/

Command to execute it

mvn flatten:clean flatten:flatten

After the execution, we can find the flattened pom file in the same location where original pom file was and with the name .flattened-pom.xml