9

Lombok在Intellij IDEA下使用eclipse compiler时的问题及解决方法

 3 years ago
source link: https://www.javatang.com/archives/2021/04/21/12233309.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
project-lombok-reduce-java-boilerplate-code-750x410.jpg

Lombok在Intellij IDEA下使用eclipse compiler时的问题及解决方法

Project Lombok可以通过注解的方式自动在JavaBean中增加gettter/setter及构造方法,使用起来可以减少代码量,非常方便。在Intellij IDEA中使用的时候需要首先安装Lombok插件,并在项目中引入lombok.jar包。

不过当Java编译器选用Eclipse Compiler for Java (ECJ)(不是Eclipse IDE)的时候(如下图所示)会出现一些问题。ECJ相较于javac有更快的编译响应速度,同JRebel一起使用的时候非常方便,更多介绍可以参考这篇What is the difference between javac and the Eclipse compiler?文章。

找不到getXXX或setXXX方法

错误提示如下:

java: The method getXxx() is undefined for the type unit.ajax.dto.PurchaseBean

首先需要确保Preferences-Build, Executing, Deployment-Compiler-Annotation Processors下面将Enable annotation processing选项勾选:

然后在Preferences-Build, Executing, Deployment-Compiler下面,设置Shared build process VM options:内如如下:

-javaagent:/Users/<your user name>/.m2/repository/org/projectlombok/lombok/1.18.16/lombok-1.18.16.jar

这里需要注意的是,-javaagent:后面一定是lombok.jar文件的绝对路径,Mac/Linux下面不支持使用~表示当前用户主目录,另外还需要注意路径中如果出现空格的话一定要用双引号将其包住,否则会出现Error opening zip file or JAR manifest missing的错误。

在Mac/Linux系统下可以使用ln命令创建一个软链接,如:

sudo ln -s ~/.m2/repository/org/projectlombok/lombok/1.18.16/lombok-1.18.16.jar /Library/lombok.jar

这样只要设定成 -javaagent:/Library/lombok.jar就可以了。

另外设置好之后,最后对项目重新进行编译。

出现 ClassFormatError

详细错误内容如下:

这个其实是lombok插件的一个bug作者承诺会在下个版本中解决,但在现在的0.34版本中还没有解决这个问题。

所幸有人在Jetbrains 社区中中提到了这个bug ,官方,已经在IDEA 2020.3及之后的版本解决了这个问题(IDEA-254725),可以先将IDEA升级到这个版本之后。


参考资料:
Lombok annotations do not compile under Intellij idea [duplicate]
“Error opening zip file or JAR manifest missing” when configuring javaagent in IntelliJ
Lombok + Eclipse compiler problem: "java.lang.ClassFormatError: Extra bytes at the end of class file"

发表评论 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

评论

显示名称 *

电子邮箱地址 *

网站网址


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK