0

Recently we have started migrating to java 17 from 8. We found some jaxb exceptions (shown below) which got resolved after adding --add-opens=java.base/java.lang=ALL-UNNAMED. I am not sure if this is correct fix. what this statement means? I know since java 9, modular concept has been introduced to have strong encapsulation. What is the correct fix?

Exception=Caused by: java.lang.ExceptionInInitializerError: Exception 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 @2a80c53

6
  • 3
    "What is the correct fix?" -- The correct fix, I believe, is for the code trying to access ClassLoader::defineClass reflectively to instead use Lookup::defineClass. I would expect jaxb to have solved this problem a long time ago. Are you using the latest version of jaxb (or at least one compatible with Java 9+)? Commented Nov 9, 2024 at 22:38
  • 2
    "What this statement means?" -- The --add-opens is telling the runtime to treat the java.lang package in the java.base module as opens to the unnamed module, regardless of what the module's actual module-info descriptor says. See Understanding Java 9 Modules for an overview on how modules work. Commented Nov 9, 2024 at 22:40
  • 1
    The unnamed module is basically all code loaded from the class-path. That includes libraries you place on the class-path, such as jaxb. That library is trying to access ClassLoader::defineClass via reflection. But that method is protected and the java.base module does not opens the java.lang package to the unnamed module. In other words, the java.base module does not allow reflective access to non-public members of the java.lang package. Hence the error. You can use --add-opens to override this. But again, the correct fix is most likely to use a newer version of jaxb. Commented Nov 13, 2024 at 19:50
  • 1
    Note the java.base module does exports the java.lang package unconditionally. That means all other modules, including unnamed ones, can both access public members of that pckage reflectively and directly reference public members of that package (and protected members in subclasses) at compile-time. But since that package is not opens, non-public members are not accessible via reflection. Commented Nov 13, 2024 at 19:54
  • 1
    As for your error, see github.com/eclipse-ee4j/jaxb-ri/issues/1197 and github.com/eclipse-ee4j/jaxb-ri/issues/1501. The first says the issue was fixed in version 2.3.1. Upgrade to at least that version to see if that solves the problem. Here's a related question: stackoverflow.com/questions/50237516/…. If you are using 2.3.1+, and are creating a so-called fat JAR file, see if adding Multi-Release: true to the JAR's manifest helps. Commented Nov 13, 2024 at 20:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.