expert
expert

Reputation: 30145

Can't compile project when I'm using Lombok under IntelliJ IDEA

I'm trying to use Lombok in my project that I'm developing using IntelliJ IDEA 11.

I've installed 3rd-party plugin for IDEA and it seems working fine because IDEA sees all autogenerated methods/fields.

So I have a class that uses Slf4j. I annotated it like this

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestClass
{
    public TestClass()
    {
        log.info("Hello!");
    }
}

But when I build my project compiler spits: cannot find symbol variable log.

Could you please tell me what I'm missing here?

Update: It turned out it's RequestFactory annotation process that fails.

input files: {com.zasutki.courierApp.server.TestServlet, com.mine.courierApp.server.model.DatastoreObject}

annotations: [javax.inject.Singleton, javax.inject.Inject, lombok.Getter, lombok.Setter, com.googlecode.objectify.annotation.Id, com.googlecode.objectify.annotation.OnSave]

Processor com.google.web.bindery.requestfactory.apt.RfValidator matches [lombok.Getter, com.googlecode.objectify.annotation.Id, javax.inject.Inject, lombok.Setter, com.googlecode.objectify.annotation.OnSave, javax.inject.Singleton] and returns false.

cannot find symbol variable log

Any ideas on workarounds?

Update2: Perhaps it's not something readers want to hear but I ended up switching to Kotlin. Do not use Lombok.

Upvotes: 291

Views: 418030

Answers (30)

camilajenny
camilajenny

Reputation: 5064

My issue was that the bundled version of Lombok plugin in IntelliJ IDEA was incompatible with the version of IntelliJ IDEA itself. I downgraded IntelliJ to 2019.1.4 and it worked.

enter image description here


it might also be a mess with Java version, Spring, and lombok for higher versions thereof, take a look Compilation error after upgrading to JDK 21 - "NoSuchFieldError: JCImport does not have member field JCTree qualid".

Those are the version I reverted to struggling with Java 23:

<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>

<java.version>17</java.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<mapstruct-processor.version>0.2.0</mapstruct-processor.version>
<lombok.version>1.18.30</lombok.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>

and it worked. See also this one How to configure Lombok with maven-compiler-plugin?.

Upvotes: 2

Neeraj Parkash Sharma
Neeraj Parkash Sharma

Reputation: 31

Lombok version image

Please make sure, you have added the ${lombok.version} in pom.xml or just replace/paste this snippet in pluggins

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>

Upvotes: 2

Grigory Kislin
Grigory Kislin

Reputation: 18030

IDEA set wrong jar processor path in this settings for multi-module maven. Correct it or set "Obtain processors from classpath" - the simplest working option

enter image description here

Upvotes: 3

Melih
Melih

Reputation: 774

I solved the problem by directing Annotation Processors to the correct jar of lombok.

Build, Execution, Deployment -> Compiler -> Annotation Processors ->

  • Enable annotation processing (check)
  • Obtain processors from project classpath (check)

Or, choose the correct jar manually. In your .m2 folder, there are different lombok jars for your project, and if you don't specify a version for this dependency, Intellij picks up a wrong one (unknown). Change this to the latest one manually, like .m2\repository\org\projectlombok\lombok<aValidVersion>

Upvotes: 1

k9ne257
k9ne257

Reputation: 21

another thing that might help is to go into .idea folder, then open compiler.xml. You should something like this:

make sure the path is correct and lombok version matches the one in your pom.xml.

Upvotes: 0

Tarun Kundhiya
Tarun Kundhiya

Reputation: 156

I faced the same problem. We need to add the annotation processor, other answer might be right with configuring it via IDE, we can also add it in build.gradle as

    annotationProcessor 'org.projectlombok:lombok'

Sample Gradle File

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-jdbc'
  implementation 'org.springframework.boot:spring-boot-starter-web'
  implementation 'org.springframework.boot:spring-boot-starter-data-jpa'


  compileOnly 'org.projectlombok:lombok'
  runtimeOnly 'com.h2database:h2'

  annotationProcessor 'org.projectlombok:lombok'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

Upvotes: 0

Antonio Silvan
Antonio Silvan

Reputation: 51

I had the same issue, in my case, I created a util class in the test package and Lombok simply didn't work. I guess you have to specifically define which packages have to run in the configuration.

But I just moved my util class to the main src folder and it worked.

Upvotes: 0

camilajenny
camilajenny

Reputation: 5064

For IntelliJ 2023.1.3, I had to close IntelliJ, remove all *.iml files, all .idea folders, rename the project folder, add lombok dependency to pom, create a new project in intellij, import a module from sources, enable annotation processing and then it finally understood what I want.

Upvotes: 0

Geoffrey
Geoffrey

Reputation: 494

In my case, to make it work on some classes defined in the tests using java code in a plain kotlin project (no android) without the kapt plugin, I had to do in my build.gradle.kts:

    dependencies {
        compileOnly("org.projectlombok:lombok:1.18.20")
        annotationProcessor("org.projectlombok:lombok:1.18.20")
        ...
        testCompileOnly("org.projectlombok:lombok:1.18.20")
        testAnnotationProcessor("org.projectlombok:lombok:1.18.20")
    }

Then "Build" -> "Rebuild project" was working.

If you will use the kapt plugin, you should be able to remove the annotationProcessor lines. But I had to deal without kapt in my case.

Upvotes: 0

elhose
elhose

Reputation: 348

Similar issue came up during SpringBoot migration to version 3.x.x+. @ConstructorBinding annotation doesn't work with Lombok, it has to be removed

Upvotes: 0

Guillermo
Guillermo

Reputation: 791

I had the same problem, but non of the answers given here worked for me. The error message I was getting on the import lombok.val imports:

intellij lombok cannot resolve symbol

I'm using IntelliJ "IDEA 2022.3.1 (Community Edition)". Apparently IntelliJ scans the classpath for annotation processing, so adding lombok not for compilation only, but also for runtime worked for me:

Use

implementation 'org.projectlombok:lombok'

instead of

compileOnly 'org.projectlombok:lombok'

Upvotes: 0

Tohid Makari
Tohid Makari

Reputation: 2494

If you already installed it, then for refresh just deselect and select Enable annotation in Intellij Settings.

Upvotes: 2

wst
wst

Reputation: 4338

It may happen that even if you have it configured properly and it is visible among the libraries and in Gradle dependencies list, IntelliJ still does not have it in class path. Or it is there, but configured with different scope (ex: test instead of compile.)

First, make sure you have plugin installed and annotation processing enabled, as stated in other answers.

If you still have annotation not recognized, place cursor on it, hit ALT+ENTER (or OPTION+ENTER) and see if you have a menu option Add library: Gradle: org.projectlombok:lombok:VERSION to class path. If you can see it, choose this one and it may solve your problem.

You may check the library and it's scope in: Project settings / Modules / Dependencies tab (search for lombok in there)

Upvotes: 1

LOrD_ARaGOrN
LOrD_ARaGOrN

Reputation: 4536

I tried enabling lambok, restarted intellij, etc but below worked for me.

Intellij Preferences ->Compiler -> Shared Build process VM Options and set it to

  -Djps.track.ap.dependencies=false

than run

mvn clean install

Upvotes: 2

hatanooh
hatanooh

Reputation: 4287

  1. add Lombok plugin.
  2. In my case, just maven clean.

Upvotes: 0

Dhiraj Surve
Dhiraj Surve

Reputation: 392

If none of the above did'nt work , then try to change File->Project Structure->Project->Project Language Level > 8 Lambda,type annotations (Not SDK Default 8)

This worked for me .

Upvotes: 1

Dan Bitter
Dan Bitter

Reputation: 259

I had to disable the @Document annotation that I'd just added. I'm converting my project to use Mongo instead of Postgres, and it was previously working, but it seems @Document conflicts with Lombok's @Getter

Upvotes: 0

user10339671
user10339671

Reputation: 69

This happened to me because the JDK version that runs the building process is too low. Lombok was built with JDK 1.7 or up. The build be must run with Java version 1.7 or up.

enter image description here

Upvotes: 0

user2021572
user2021572

Reputation: 5282

I have fixed it in IDEA 12 by setting check box Enable annotation processing in:

Settings -> Compiler -> Annotation Processors

For IDEA 2016.2:

Preferences... > Build, Execution, Deployment > Compiler > Annotation Processors

After enabling, run Build -> Rebuild Project to have annotations recognized and eliminate errors.


For IDEA 2019.2.1, depending on how the project is configured, installing the Project Lombok plugin may not be sufficient. Here is another way to use Project Lombok with IntelliJ IDEA:

  1. Visit https://projectlombok.org/download
  2. Download the JAR file into the project lib directory (e.g., $HOME/dev/java/project/libs).
  3. Start the IDE.
  4. Click File ๐Ÿ – Settings.
  5. Expand Build, Execution, Deployment ๐Ÿ – Compiler ๐Ÿ – Annotation Processors.
  6. Ensure Enable annotation processing is checked.
  7. Ensure Store generates sources relative to is selected based on the project's module settings (if Module output directory doesn't work, come back and try the other setting).
  8. Click Apply.
  9. Click Plugins.
  10. Click Marketplace.
  11. Set search field to: lombok
  12. Install Lombok.
  13. Click OK.
  14. Restart the IDE if prompted.
  15. Click File ๐Ÿ – Project Structure.
  16. Select Libraries.
  17. Click the + symbol to add a new project library (or press Alt+Insert).
  18. Select Java.
  19. Set the path to: $HOME/dev/java/project/libs/lombok.jar
  20. Click OK.
  21. Select the modules to apply.
  22. Click OK.
  23. Optionally, rename lombok to Project Lombok 1.18.8.
  24. Click OK.

The project can now import from the lombok package and use Project Lombok annotations (e.g., lombok.Setter and lombok.Getter).

Upvotes: 525

S Kampen
S Kampen

Reputation: 41

I don't think I read my final step in the answers yet. (Mac + IntelliJ Ultimate 2020.1) Its just a silly cause in my case, but those are the ones that can take up most time because the error doesnt directly refer to it.

The same lombok error appeared to me after deleting and recloning the project. After doing the steps mentioned earlier in this thread I still had the error, I then discovered my SKD was defaulted to version 11. I changed this back to 1.8 and everything worked again.

File --> Project Settings --> Project I changed the Project SDK and the Project language level to 1.8

PS the location for the default settings on the mac is different in this IntelliJ version than mentioned before : File --> New Project Settings --> Preferences for new Projects --> Build, Execution, Deployment --> Compiler --> Annotation Processors --> 'check' Enable annotation processing

Hope this helps anybody

Upvotes: 1

Sasi Kumar M
Sasi Kumar M

Reputation: 2630

The Jetbrains IntelliJ IDEA editor is compatible with lombok without a plugin as of version 2020.3.

I was using 2020.2 version, i updated to 2020.3 it worked just like that.

Upvotes: 1

Enrique S. Filiage
Enrique S. Filiage

Reputation: 810

I'm using IntelliJ IDEA 2020.3 (Community Edition)

Here, besides install the Lombok plugin and enable annotations (explained by other answers). I also needed to set the flag -Djps.track.ap.dependencies=false to the Build Process Optionยน.

I didn't need to use the -javaagent approach, neither setup the classpath.

ยน. Go to: File | Settings | Build, Execution, Deployment | Compiler | "Shared build process VM options" field

References:

Upvotes: 27

Blink
Blink

Reputation: 1554

After enabling annotator processors I had to update to the newest version of lombok:

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

Intellij version:

IntelliJ IDEA 2020.3 (Community Edition)
Build #IC-203.5981.155, built on November 30, 2020
Runtime version: 11.0.9+11-b1145.21 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 10.16
GC: ParNew, ConcurrentMarkSweep
Memory: 1981M
Cores: 8
Non-Bundled Plugins: org.intellij.plugins.hcl, Lombook Plugin, org.sonarlint.idea, in.1ton.idea.spring.assistant.plugin, org.jetbrains.kotlin, gherkin, cucumber-java

Upvotes: 0

Tugrul
Tugrul

Reputation: 1808

If you did everything mentioned in this question and It's still failing, don't forget to remove /target folder under your projects. And If it's still failing, restart your IDE. And If it's still failing restart your computer.

Upvotes: 2

Including the following in the pom.xml is what worked for me:

<build>
        <defaultGoal>spring-boot:run</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
...
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>       
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
</build>

Upvotes: 19

For IntelliJ IDEA 2020.1.1 enabling Kotlin plugin fixed this issue.

Upvotes: 1

Sagiruddin Mondal
Sagiruddin Mondal

Reputation: 5797

For me what worked:

  1. I uninstalled the installed the Lombok plugin freshly
  2. I ticked "Enable Annotation Plugin"
  3. I selected "Obtain processor from the project classpath" in the same page

Upvotes: 1

Vaibhav K
Vaibhav K

Reputation: 51

Apart from mentioned in all answers I have to add the below code in pom.xml configuration to makes mvn clean install work. Before adding this code I was getting cannot found symbol for getters and setters.

                    <annotationProcessorPath>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.8</version>
                    </annotationProcessorPath>

Upvotes: 2

Stepan Mozyra
Stepan Mozyra

Reputation: 267

After trying all the suggestions here, I have also find another kind of solution. It seems that sometimes IDEA can not obtain processors from project classpath.

So, on the Annotation Processors settings tab, you have to manually specify Processor path.

Upvotes: 1

Valeriy K.
Valeriy K.

Reputation: 2904

in the latest Gradle version you should use annotationProcessor:

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok:1.18.8'

Upvotes: 38

Related Questions