Maven之java.lang.ExceptionInInitializerError: com.sun.tools.javac.code.TypeTags异常分析

问题分析 同时被 2 个专栏收录
72 篇文章 0 订阅
139 篇文章 0 订阅

问题引入

在项目中,编译代码过程中出现了如下问题:


[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project xxx: Fatal error compiling: java.lang.ExceptionInInitializerError: com.sun.tools.javac.code.TypeTags -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project mall-tms-waybill-dao: Fatal error compiling
       at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
       at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
       at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
       at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
       at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
       at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
       at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
       at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
       at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
       at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
       at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
       at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
       at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
       at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
       at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.base/java.lang.reflect.Method.invoke(Method.java:564)
       at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
       at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
       at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
       at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Fatal error compiling
       at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:1145)
       at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:187)
       at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
       at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
       ... 20 more
Caused by: org.codehaus.plexus.compiler.CompilerException: java.lang.ExceptionInInitializerError
       at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess(JavaxToolsCompiler.java:173)
       at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile(JavacCompiler.java:174)
       at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:1134)
       ... 23 more
Caused by: java.lang.RuntimeException: java.lang.ExceptionInInitializerError
       at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.handleExceptions(JavacTaskImpl.java:163)
       at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:100)
       at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:94)
       at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess(JavaxToolsCompiler.java:126)
       ... 25 more
Caused by: java.lang.ExceptionInInitializerError
       at lombok.javac.handlers.HandleGetter.<clinit>(HandleGetter.java:298)
       at java.base/java.lang.Class.forName0(Native Method)
       at java.base/java.lang.Class.forName(Class.java:468)
       at lombok.core.SpiLoadUtil$1$1.next(SpiLoadUtil.java:111)
       at lombok.javac.HandlerLibrary.loadAnnotationHandlers(HandlerLibrary.java:170)
       at lombok.javac.HandlerLibrary.load(HandlerLibrary.java:155)
       at lombok.javac.JavacTransformer.<init>(JavacTransformer.java:43)
       at lombok.javac.apt.LombokProcessor.init(LombokProcessor.java:86)
       at lombok.core.AnnotationProcessor$JavacDescriptor.want(AnnotationProcessor.java:87)
       at lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:141)
       at lombok.launch.AnnotationProcessorHider$AnnotationProcessor.init(AnnotationProcessor.java:53)
       at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.<init>(JavacProcessingEnvironment.java:702)
       at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.next(JavacProcessingEnvironment.java:829)
       at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:925)
       at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1269)
       at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1383)
       at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1261)
       at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:935)
       at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:104)
       at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.handleExceptions(JavacTaskImpl.java:147)
       ... 28 more
Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.code.TypeTags
       at java.base/java.lang.ClassLoader.findClass(ClassLoader.java:719)
       at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
       at lombok.launch.ShadowClassLoader.loadClass(ShadowClassLoader.java:418)
       at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
       at java.base/java.lang.Class.forName0(Native Method)
       at java.base/java.lang.Class.forName(Class.java:377)
       at lombok.javac.JavacTreeMaker$SchroedingerType.getFieldCached(JavacTreeMaker.java:156)
       at lombok.javac.JavacTreeMaker$TypeTag.typeTag(JavacTreeMaker.java:244)
       at lombok.javac.Javac.<clinit>(Javac.java:154)
       ... 48 more
Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.code.TypeTags
       at java.base/java.lang.ClassLoader.findClass(ClassLoader.java:719)
       at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
       at lombok.launch.ShadowClassLoader.loadClass(ShadowClassLoader.java:418)
       at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
       at java.base/java.lang.Class.forName0(Native Method)
       at java.base/java.lang.Class.forName(Class.java:377)
       at lombok.javac.JavacTreeMaker$SchroedingerType.getFieldCached(JavacTreeMaker.java:156)
       at lombok.javac.JavacTreeMaker$TypeTag.typeTag(JavacTreeMaker.java:244)
       at lombok.javac.Javac.<clinit>(Javac.java:154)
       ... 48 more

其中核心的错误信息如下:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project mall-tms-waybill-dao: Fatal error compiling: java.lang.ExceptionInInitializerError: com.sun.tools.javac.code.TypeTags -> [Help 1]

从直观的错误信息判断,好像是与maven-compiler-plugin有关,从root cause中可知,其底层原因是:

Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.code.TypeTags

这个类找不到,这个是什么类?从类名来看是JDK中sun的私有类,可以判断其与JDK有关系,或类在不同JDK下的兼容性有关

问题分析

网络上的文章大部分是提到Lombok的版本,检查了一下:

<dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.16</version>
 </dependency>

这个版本是最新的。
检查了一下maven的基本信息:

mvn -version
输出信息如下:

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/Cellar/maven@3.3/3.3.9_1/libexec
Java version: 15.0.1, vendor: N/A
Java home: /usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home
Default locale: zh_CN_#Hans, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.5", arch: "x86_64", family: "mac"

之前使用了brew安装maven:

brew install maven@3.3

默认情况下直接安装了maven

尝试解决

尝试1: 重新安装maven至最新版本

brew install maven

问题依旧。
尝试2: 重新安装maven,指定–ignore-dependencies,不安装依赖

brew install --ignore-dependencies maven

安装输出信息如下:

Warning: `--ignore-dependencies` is an unsupported Homebrew developer flag!
Adjust your PATH to put any preferred versions of applications earlier in the
PATH rather than using this unsupported flag!

查看brew install的帮助文档,信息如下:

brew install -h

输出信息如下:

--ignore-dependencies        An unsupported Homebrew development flag to
                                   skip installing any dependencies of any
                                   kind. If the dependencies are not already
                                   present, the formula will have issues. If
                                   you're not developing Homebrew, consider
                                   adjusting your PATH rather than using this
                                   flag.

结论: 依然无法解决问题。

正确姿势

那就尝试手动安装maven吧。

  1. 下载maven(maven.apache.org)
  2. 解压安装
>  tar -zxvf apache-maven-3.6.3.tar.gz
  1. 设置环境变量
    写入~/.zshrc

export MAVEN_HOME=~/apache-maven-3.6.3
export PATH= P A T H : PATH: PATH:MAVEN_HOME/bin

生效设置:

source ~/.zshrc

其他类似的文件: ~/.zshenv

检查maven的版本信息:

mvn -version

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Applications/apache-maven-3.6.3
Java version: 1.8.0_282, vendor: Oracle Corporation, runtime: /usr/local/Cellar/openjdk@8/1.8.0+282/libexec/openjdk.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.5", arch: "x86_64", family: "mac"

重新编译执行即可正确通过

总结

这里的问题是maven使用的JDK版本与系统默认代码编译的JDK版本不一致导致的。

参阅文档

  1. 正确安装maven姿势
  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值