spring-bootmavenintellij-ideamaven-jaxb2-pluginjava-17

java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)


I am trying to update my SpringBoot maven project to Java 17.

<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>

enter image description here

I had working maven-jaxb2-plugin plugin defined which created java classes successfully from XSD files.

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.14.0</version>
    <executions>
        <execution>
            <id>my_schema</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaDirectory>src/main/resources/my/files</schemaDirectory>
                <schemaIncludes>
                    <include>*.xsd</include>
                </schemaIncludes>
                <bindingDirectory>src/main/resources/my/files</bindingDirectory>
                <bindingIncludes>
                    <include>bindings.xml</include>
                </bindingIncludes>
                <args>
                    <arg>-extension</arg>
                    <arg>-Xnamespace-prefix</arg>
                </args>
            </configuration>
        </execution>
    </executions>
</plugin>

But since the upgrade of java and the plugin I cannot do it.

I definied the necessary dependencies for the plugin:

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>3.0.2</version>
</dependency>
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>3.0.2</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>3.0.1</version>
</dependency>

But I still receive an unknown error during maven build:

Oct 19, 2021 10:07:06 PM com.sun.xml.bind.v2.runtime.reflect.opt.Injector <clinit>
SEVERE: null
java.security.PrivilegedActionException: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:573)
    at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.<clinit>(Injector.java:197)
    at com.sun.xml.bind.v2.runtime.reflect.opt.AccessorInjector.prepare(AccessorInjector.java:81)
    at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:179)
    at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:285)
    at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.<init>(TransducedAccessor.java:235)
    at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:175)
    at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.calcTransducer(RuntimeClassInfoImpl.java:245)
    at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.getTransducer(RuntimeClassInfoImpl.java:219)
    at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createTransducer(RuntimeModelBuilder.java:145)
    at com.sun.xml.bind.v2.model.impl.SingleTypePropertyInfoImpl.getTransducer(SingleTypePropertyInfoImpl.java:140)
    at com.sun.xml.bind.v2.model.impl.RuntimeAttributePropertyInfoImpl.link(RuntimeAttributePropertyInfoImpl.java:78)
    at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.link(ClassInfoImpl.java:1272)
    at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.link(RuntimeClassInfoImpl.java:197)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.link(ModelBuilder.java:454)
    at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.link(RuntimeModelBuilder.java:133)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:469)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:303)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:139)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1156)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:165)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:297)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:286)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:409)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
    at com.sun.tools.xjc.reader.xmlschema.bindinfo.BindInfo.getCustomizationContext(BindInfo.java:336)
    at com.sun.tools.xjc.reader.xmlschema.bindinfo.BindInfo.getCustomizationUnmarshaller(BindInfo.java:362)
    at com.sun.tools.xjc.reader.xmlschema.bindinfo.AnnotationParserFactoryImpl$1.<init>(AnnotationParserFactoryImpl.java:85)
    at com.sun.tools.xjc.reader.xmlschema.bindinfo.AnnotationParserFactoryImpl.create(AnnotationParserFactoryImpl.java:84)
    at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.createAnnotationParser(NGCCRuntimeEx.java:401)
    at com.sun.xml.xsom.impl.parser.state.annotation.action0(annotation.java:89)
    at com.sun.xml.xsom.impl.parser.state.annotation.enterElement(annotation.java:114)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:422)
    at com.sun.xml.xsom.impl.parser.state.NGCCHandler.spawnChildFromEnterElement(NGCCHandler.java:114)
    at com.sun.xml.xsom.impl.parser.state.Schema.enterElement(Schema.java:317)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:422)
    at com.sun.xml.xsom.impl.parser.state.NGCCHandler.revertToParentFromEnterElement(NGCCHandler.java:151)
    at com.sun.xml.xsom.impl.parser.state.foreignAttributes.enterElement(foreignAttributes.java:91)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:422)
    at com.sun.xml.xsom.impl.parser.state.NGCCHandler.spawnChildFromEnterElement(NGCCHandler.java:114)
    at com.sun.xml.xsom.impl.parser.state.Schema.enterElement(Schema.java:229)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:422)
    at com.sun.xml.xsom.impl.parser.state.Schema.enterElement(Schema.java:273)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:422)
    at com.sun.xml.xsom.impl.parser.state.Schema.enterElement(Schema.java:309)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:422)
    at com.sun.xml.xsom.impl.parser.state.Schema.enterElement(Schema.java:293)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:422)
    at com.sun.xml.xsom.impl.parser.state.Schema.enterElement(Schema.java:221)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.sendEnterElement(NGCCRuntime.java:422)
    at com.sun.xml.xsom.impl.parser.state.Schema.enterElement(Schema.java:257)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.startElement(NGCCRuntime.java:263)
    at java.xml/org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:539)
    at com.sun.tools.xjc.util.SubtreeCutter.startElement(SubtreeCutter.java:108)
    at com.sun.tools.xjc.reader.ExtensionBindingChecker.startElement(ExtensionBindingChecker.java:150)
    at java.xml/org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:539)
    at com.sun.tools.xjc.reader.xmlschema.parser.IncorrectNamespaceURIChecker.startElement(IncorrectNamespaceURIChecker.java:128)
    at java.xml/org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:539)
    at com.sun.tools.xjc.reader.xmlschema.parser.CustomizationContextChecker.startElement(CustomizationContextChecker.java:193)
    at java.xml/org.xml.sax.helpers.XMLFilterImpl.startElement(XMLFilterImpl.java:539)
    at com.sun.tools.xjc.reader.internalizer.DOMForestScanner$LocationResolver.startElement(DOMForestScanner.java:147)
    at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:244)
    at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:281)
    at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:250)
    at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:127)
    at com.sun.tools.xjc.reader.internalizer.DOMForestScanner.scan(DOMForestScanner.java:92)
    at com.sun.tools.xjc.reader.internalizer.DOMForestScanner.scan(DOMForestScanner.java:100)
    at com.sun.tools.xjc.reader.internalizer.DOMForestParser.parse(DOMForestParser.java:104)
    at com.sun.tools.xjc.ModelLoader$XMLSchemaParser.parse(ModelLoader.java:251)
    at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.parseEntity(NGCCRuntimeEx.java:381)
    at com.sun.xml.xsom.impl.parser.ParserContext.parse(ParserContext.java:128)
    at com.sun.xml.xsom.parser.XSOMParser.parse(XSOMParser.java:171)
    at com.sun.xml.xsom.parser.XSOMParser.parse(XSOMParser.java:160)
    at com.sun.tools.xjc.ModelLoader.createXSOM(ModelLoader.java:516)
    at com.sun.tools.xjc.ModelLoader.loadXMLSchema(ModelLoader.java:360)
    at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:162)
    at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:117)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.loadModel(XJC23Mojo.java:50)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.doExecute(XJC23Mojo.java:40)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.doExecute(XJC23Mojo.java:28)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute(RawXJC2Mojo.java:478)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute(RawXJC2Mojo.java:320)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
Caused by: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
    at java.base/java.lang.Class.getMethod(Class.java:2227)
    at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:201)
    at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:197)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
    ... 109 more

And I cannot find any useful tipp on the web. Can anyone help me how to fix this issue?


Solution

  • It compiles, when you'll add jaxb-runtime dependency, as below:

        <build>
            <plugins>
                <plugin>
                    <groupId>org.jvnet.jaxb2.maven2</groupId>
                    <artifactId>maven-jaxb2-plugin</artifactId>
                    <version>0.14.0</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.glassfish.jaxb</groupId>
                            <artifactId>jaxb-runtime</artifactId>
                            <version>${jaxb-api.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    

    Notice, that I didn't close all open tags, because those are irrelevant to the question. Those are just open for clarity of where to put jaxb-runtime dependency. I used 2.3.1 version, and it compiled.