137

I have a maven project that builds with no problems from the command line. However, when I build it with IntelliJ, I get the error:

java: FileName.java:89: cannot find symbol
symbol  : variable log

There is no log defined or imported in the java file, but there is a

@Slf4j
final public class FileName {

statement before the class body which should define the log class.

In the project structure window, classes for:

Maven: org.slf4j:jcl-over-slf4j:1.6.1
Maven: org.slf4j:slf4j-api:1.6.6
Maven: org.slf4j:slf4j-log4j12:1.6.6
Maven: org.slf4j:slf4j-simple:1.6.6

are listed under libraries and are indicated as having been downloaded and available.

Any idea why this would build with maven through the command line, but not through IntelliJ and how to resolve the issue?

user1991839
  • 1,643
  • 2
  • 12
  • 11

30 Answers30

194

In addition to having Lombok plugin installed, also make sure that the "Enable annotation processing" checkbox is ticked under:

Preferences > Compiler > Annotation Processors

Note: starting with IntelliJ 2017, the "Enable Annotation Processing" checkbox has moved to:

Settings > Build, Execution, Deployment > Compiler > Annotation Processors
akki
  • 1,188
  • 18
  • 29
Alexander Zagniotov
  • 1,979
  • 1
  • 10
  • 8
  • 1
    I don't have annotation processing enabled and works :| I do have lombok plugin intalled. – Ben George Jul 21 '16 at 06:57
  • I installed the plugin and it worked without changing this setting. However, I decided to change it for good measure. – sheldonkreger Aug 10 '17 at 17:44
  • 6
    Using IntelliJ 2017.2 and Lombok plugins 0.14.16, setting "Enable annotation processing" was necessary and the checkbox is found under Preferences -> Build, Execution, Deployment -> Compiler -> Annotation Processors. – Niemi Sep 04 '17 at 14:19
  • 1
    IntelliJ 2018.2, lombok plugin installed 1.14. I still had to enable annotation processing in IntelliJ. File->Settings->Build, Execution, Deployment->Compiler->Annotation Processors. IntelliJ is not really winning points with me. – demaniak Sep 10 '18 at 07:16
  • My new project did not have a slf4j-* dependency created yet. Once i added it Idea recognized the log and its methods. – codester Oct 08 '20 at 19:02
36

Presumably, that's the Lombok @Slf4j annotation you're using. You'll need to install the Lombok plugin in IntelliJ if you want IntelliJ to recognize Lombok annotations. Otherwise, what do you expect if you try to use a field that doesn't exist?

Ryan Stewart
  • 115,853
  • 19
  • 167
  • 192
  • 1
    I have the Lombok plugin installed. There doesn't appear to be much in terms of configuration options. Perhaps it is a bug in the plugin? – user1991839 Feb 14 '13 at 02:31
  • 1
    I've used it successfully with IDEA 11 and 12. Occasionally, something seems to "forget" about Lombok, and I get a bunch of red lines in the source code, but it still builds for me. It comes and goes, though, so the plugin definitely isn't bug-free. – Ryan Stewart Feb 14 '13 at 02:34
  • Do you know if there is a workaround to get past this? Like possibly informing IntelliJ of the Lombok libraries without going through the plugin? I'm trying to use IntelliJ for debugging tests and this is making it difficult for me to use it for that – user1991839 Feb 14 '13 at 06:42
  • Sorry, can't help you there. I haven't used Lombok very much yet. I've just tried it out in a few places. I'd check out the plugin's [project page](http://code.google.com/p/lombok-intellij-plugin/), but I have a feeling there isn't a very big community around it yet. – Ryan Stewart Feb 14 '13 at 06:52
25

In Intellij version 2016, 2017, enable Preferences -> Compiler -> Annotation Processors does not work for me!

The following additional checkbox helps: enter image description here

Tim Long
  • 1,768
  • 1
  • 19
  • 24
19

2019:

Get a plugin and you are sorted...

File > Settings > Plugins

enter image description here

Witold Kaczurba
  • 7,871
  • 2
  • 47
  • 52
14

There is the following step to be followed here:

Step 1. Enabled annotation processing for your project under File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor

Screenshot enter image description here

Step 2. Install lombok plugin in IntelliJ IDE after that restart IDE. Screenshot

enter image description here

Step 3. Add the dependency in build.gradle file.

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

or https://developervisits.wordpress.com/2020/09/16/building-with-lomboks-slf4j-and-intellij-cannot-find-symbol-log/

hope this answer is helpful for you.

Praveen Kumar Verma
  • 2,115
  • 1
  • 13
  • 27
13

I might be ungraving a dead topic but a simple solution is to check in your dependencies (Maven's pom for exemple) if you are including logback-core and logback-classic.

Slf4j is just the interface, you need the concrete implementation behind it to work.

I've been tricked twice with IDEA messing it up, now I'm good to go :D

Ethenyl
  • 581
  • 11
  • 17
11

Worked for me!!! It was failing on CircleCI & on Jenkins as well.

If you're a Gradle User try add the following into your dependencies:

dependencies {
    //Other Dependencies >>

    //LOMBOK Dependencies
    annotationProcessor 'org.projectlombok:lombok'
    compileOnly 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'
    testCompileOnly 'org.projectlombok:lombok'
}
Noel Yap
  • 15,499
  • 17
  • 77
  • 123
S34N
  • 5,119
  • 4
  • 29
  • 38
  • The last 3 items are what I needed to add when using Gradle. For a previous project I had already taken care of the Lombok plugin, and the annotation processor checkbox for a previous project for which we used Maven. I encountered this error after using IntelliJ to create a Spring Boot project with Lombok selected; it included the compileOnly, and annotationProcessor lines, but not the test* lines. – Xenson Aug 13 '19 at 23:12
8

So if the issue persists even after enabling the annotation processing and installing the Lombok plugin.

There's an issue with IDEA 2020.3 and Lombok, you could fix this by following this fix.

basically, add -Djps.track.ap.dependencies=false to the VM options, you can find it in: Preferences -> Compiler. Named 'Shared build process VM Options'

mohRamadan
  • 393
  • 4
  • 12
5

Itsn't a IntelliJ problem. If you try under console, run mvn install, also breaks. All annotations from lombok.extern needed add dependencies. This package groups the next annotations:

  • CommonsLog
  • Flogger
  • Log
  • JBossLog
  • Log4
  • Log4j2
  • Slf4j
  • XSlf4j

For example, for Slf4j it's necessary add this dependency to your pom.xml

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j.version}</version>
</dependency>
albertoiNET
  • 1,189
  • 17
  • 28
5

If you are using maven, try adding Lombok path to maven-compiler-plugin list of annotation processor as shown below.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.3.0.Final</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.10</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>

Change the version as per your version of Lombok. Other than that ensure you have done the following

  • installed the Lombok plugin for Intellij.
  • Enabled annotation processing for your project under File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor. For me both, Obtain processors from project classpath and Processor path is working. So not sure what will work for you, but try whichever works.

And rather than shooting in the dark for hours. Reading a little bit how annotation processors work and are used by compiler may help. so have quick read below.

http://hannesdorfmann.com/annotation-processing/annotationprocessing101

Meena Chaudhary
  • 6,515
  • 8
  • 40
  • 70
4

In IDEA 13 this seems to no longer be an issue, you just have to have the Lombok plugin installed.

f120146
  • 41
  • 1
4

I've just installed the latest idea verion 2108.1 and found this issue, after installed lombok plugin and restart the Idea resolve it.

mefor sy
  • 71
  • 2
4

I tried almost all of the mentioned answers but nothing worked for me. My gradle build was failing every time. Just found this solution:

Add annotationProcessor 'org.projectlombok:lombok' in your build.gradle.

This worked for me.

4

Removing the @Slf4J annotation from the class and then re-adding it worked for me.

cecile
  • 41
  • 1
3

1 My gradle lombok dependecies:

implementation 'org.projectlombok:lombok:1.18.10'
annotationProcessor 'org.projectlombok:lombok:1.18.10'

2 After enabling "Annotations..." in IDEA (Settings), taking into account that you have installed Lombok plugin, that resolved my the same issue

AlexPes
  • 646
  • 7
  • 14
2

This worked for me : File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor

Tick on 'enable annotation processing'. Apply

Close

Sumukh Bhandarkar
  • 366
  • 1
  • 5
  • 13
1

I was seeing this issue with an older version of Lombok when compiling under JDK8. Setting the project back to JDK7 made the issue go away.

John Chapman
  • 838
  • 3
  • 14
  • 28
1

This won't have been OP's problem, but for anyone else who tries everything with no success:

I had similar symptoms. Whenever I built after a mvn clean, it wouldn't find log, or getXYZ(), or builder(), or anything.

[ERROR]   symbol:   variable log
[ERROR]   location: class com.example.MyClass
[ERROR] /Path/To/Some/Java/src/main/com/example/MyClass.java:[30,38] cannot find symbol
[ERROR]   symbol:   method builder()
[ERROR]   location: class com.example.MyClass

After reading every answer I could find about QueryDSL/JPA/Hibernate/Lombok/IntelliJ/Maven issues to no avail, I worked out that the culprit was a single static import of a @Getter method that was annotated on a static field.

Spring 1.15.14.RELEASE, Intellij 2019.1.1

@SpringBootApplication
public class BarApplication implements ApplicationContextAware {
  @Getter
  private static ApplicationContext applicationContext;

  // ... start Spring application, and grab hold of ApplicationContext as it comes past
}
import ...
import static BarApplication.getApplicationContext;

@Slf4j
public class IMakeItAllFail {
   public IMakeItAllFail() {
      log.info("{}", getApplicationContext());
   }
}
@Slf4j
public class Foo {
  Foo() {
    log.info("I fail to compile but I have nothing to do with the other classes!");
  }
}
Corwin Newall
  • 484
  • 6
  • 16
1

After enabling annotation processors and installing the lombok plugin, it still didn't work. We worked around it by checking the Idea option "Delegate IDE build to gradle"

0

What sorted out things for me was to tick the checkbox "Use plugin registry" in Maven settings.

The path is: File -> Preferences -> Build, Execution, Deployment -> Build Tools -> Maven

Radu Ciobanu
  • 442
  • 5
  • 8
0

Delete .idea folder and .iml files in each module and rebuild the solution.

Rajiv Singh
  • 654
  • 7
  • 13
0

I had Lombok plugin, annotations enabled, it was compiling from command line - everything and it still did not see my project as maven (all maven dependencies were red in source files). Then I clicked SHIFT twice and searched for 'maven' and among results there was 'Reload all Maven Projects'. After running it Maven tab appeared and I was able to compile, and all red underlining in source code disappeared.

Kirill G.
  • 763
  • 1
  • 9
  • 23
0

A simple thing but I figured it out is: I missed adding @Log to the class.

@Log
public class YourClassName {


}

It may help someone.

Shailendra Madda
  • 16,925
  • 14
  • 71
  • 115
0

I have the same issue; I use gradle and IDEA;

It turns out that, it's caused by the wrong version of gradle.

In gradle\wrapper\gradle-wrapper.properties, it is:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-bin.zip

However, I specified the version in IDEA to be

D:\Library\gradle-5.2.1

After lower the gradle version to be 4.10.x, the issue is gone.

yinhaomin
  • 183
  • 1
  • 4
0

Try to create lombok.config file under project base directory and provide lombok.log.fieldName value.

Example: lombok.log.fieldName = LOG

Taras Melnyk
  • 2,353
  • 3
  • 27
  • 31
0

I had the same kind of error, right after a warning like the following:

java: You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.
  Your processor is: com.sun.proxy.$Proxy27
  Lombok supports: OpenJDK javac, ECJ

In my case, it was an unfortunate combination of lombok < 1.18.16 and IDEA 2020.3.

Cos64
  • 1,437
  • 1
  • 18
  • 29
0

This is another related post Lombok not working with IntelliJ 2020.3 Community Edition which may resolve the question when user use lombook and IntelliJ 2020.3 CommunityEdition.

bcjohn
  • 1,264
  • 5
  • 16
0

Here's the problem I had why IntelliJ didn't find my maven dependency: I had a duplicate tag "dependencies" somewhere else in the pom.

MJL
  • 113
  • 2
  • 8
0

Use this dependency and try:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.30</version>
</dependency>
Arefe
  • 6,963
  • 10
  • 60
  • 110
0

IDEA 2021 and still have the issue, to solve it make sure that you have the last version of lombok in your project dependencies.

akuma8
  • 2,822
  • 1
  • 25
  • 55