IntelliJ IDEA运行Groovy报错:NoClassDefFoundError: Unable to load class org.apache.groovy.jaxb.extensions.JaxbExtensions due to missing dependency javax/xml/bind/JAXBContext

时间:2021-08-15 18:31:47

运行环境

  • IntelliJ IDEA Community 2020.1(社区版)
  • JDK 11
  • Groovy3.0.9

报错详情

Information:Groovyc: While compiling groovy-tutorial:java.lang.NoClassDefFoundError: Unable to load class org.apache.groovy.jaxb.extensions.JaxbExtensions due to missing dependency javax/xml/bind/JAXBContext
at org.codehaus.groovy.vmplugin.v8.Java8.configureClassNode(Java8.java:460)
at org.codehaus.groovy.ast.ClassNode.lazyClassInit(ClassNode.java:273)
at org.codehaus.groovy.ast.ClassNode.getMethods(ClassNode.java:395)
at org.codehaus.groovy.transform.stc.AbstractExtensionMethodCache.scan(AbstractExtensionMethodCache.java:116)
at org.codehaus.groovy.transform.stc.AbstractExtensionMethodCache.getMethods(AbstractExtensionMethodCache.java:97)

原因

原因是Java 9+,是没有安装JAXB,即缺少依赖项 javax/xml/bind/JAXBContext ,无法加载类 org.apache.groovy.jaxb.extensions.JaxbExtensions。

解决方法

  • 方法一:SDK降级到JDK 8
  • 方法二:添加Groovy SDK 提供的 JAXB 依赖添加至 IntelliJ IDEA 项目

这里只说如何Groovy SDK 提供的 JAXB 依赖添加至 IntelliJ IDEA Community项目

File -》 Project Structure,选择Global Libraries:

IntelliJ IDEA运行Groovy报错:NoClassDefFoundError: Unable to load class org.apache.groovy.jaxb.extensions.JaxbExtensions due to missing dependency javax/xml/bind/JAXBContext

添加groovy安装目录下的lib/extras-jaxb,如上图。