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

5

29

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?

Chemnitz answered 19/10, 2021 at 20:24 Comment(3)
JDK-8193033 remove terminally deprecated sun.misc.Unsafe.defineClass; update the software using this method to a newer version not using it (assuming there is such a newer version; otherwise you have to stay with the older Java version).Interstitial
I am not using this method. The plugin uses it, and it fails. I am fed up with these updates, they always mess up my applications..Chemnitz
I didn’t say that you are using this method. I said “update the software using this method” whereas software includes libraries as well as any tools potentially using it. I also accounted for the possibility that the library outside your control hasn’t a suitable update (“otherwise you have to stay with the older Java version”). If this is annoying, tell that the library authors who didn’t get the message. Unsafe.defineClass never was part of an official API. A clean alternative exists since Java 9. But anyway, did anyone force you to update to Java 17?Interstitial
D
28

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.

Diaspore answered 25/10, 2021 at 6:28 Comment(5)
worked also with 2.3.5, thanks!Chemnitz
You are the G.O.A.T. man! Thank You !!!Applesauce
please, please, always write a complete answer, all tags, close tags etc.Playreader
@filosofisto, I've added enclosing tags.Diaspore
I've added an answer for reference to tell about the plugin's last-year updates. Feel free to read :)Womanly
T
17

I had a similar error message when migrating my projects and libraries from java 16 to java 17. I was using the -illegal-access=permit option and it was removed in java 17. The above answer didn't solve my problem. My situation was like this. I have a library (let's say x) and an application (let's say y). Application y uses the library x. I added the following dependency to the library x and the problem was solved.

 <dependency>
     <groupId>org.glassfish.jaxb</groupId>
     <artifactId>jaxb-runtime</artifactId>
     <version>2.3.1</version>
 </dependency>

But if I add this dependency directly to application y instead of library x, the problem would not be solved.

Thousandth answered 24/12, 2021 at 10:49 Comment(2)
Where To add that in the application Y, IF Y is a SOAP webservice (not own) ?Houston
This worked for me but the 4.0.x version of Glassfish didn't work.Intermigration
P
8

Upgrading to a newer version helped me.

<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.15.1</version>
Permittivity answered 14/9, 2022 at 9:10 Comment(1)
Thanks @Permittivity for pointing out the new version. I've added another answer for reference to tell about the plugin's last-year updates. Feel free to read :)Womanly
W
2

Just to inform you that the latest JDK where supported starting from 0.15.x version of the plugin without overriding the jaxb version used in it.

You can also follow the migration guide that has been written in order to move to the latest version, according to the targeted version of the JAXB-API

  • Use v2 of the Jaxb-Tools ecosystem for JAXB 2.3 (JavaEE 8, JDK8)
  • Use v3 of the Jaxb-Tools ecosystem for JAXB 3 (JakartaEE 9, JDK8)
  • Use v4 of the Jaxb-Tools ecosystem for JAXB 4 (JakartaEE 10, JDK11)

We also did many bugfixes / enhancements in the v2 (and so upwards versions) and keep active support on v2 and v4 versions of the ecosystem (since JAXB3 itself is not actively maintained in favor of JAXB4).

Feel free to create issue in JAXB Tools repository, which is the former maven-jaxb2-plugin repository in which we merged all related jaxb-tools (that explains the new repository name) that were maintained by Alexey Valikov.

All the versions of the plugin are now tested against JDK8 (except v4 which is JDK11 baseline), JDK11, JDK17 and latest current LTS which is JDK21.

Womanly answered 1/12, 2023 at 10:47 Comment(0)
E
1

I had a similar issue as well ... in my case I had to add only the following dependency:

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>3.0.2</version>
        </dependency>
Electoral answered 4/3, 2023 at 21:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.