172

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,993
  • 2
  • 14
  • 11

37 Answers37

221

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
  • 2,021
  • 1
  • 24
  • 35
Alexander Zagniotov
  • 2,329
  • 1
  • 12
  • 9
  • 2
    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
  • 2
    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
  • doesnt work doesnt build – Philip Rego Apr 08 '22 at 16:42
43

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
  • 126,015
  • 21
  • 180
  • 199
  • 3
    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
43

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'

In case you are using lombok in tests you need to add:

testCompileOnly 'org.projectlombok:lombok:1.18.12'  
testAnnotationProcessor '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,988
  • 2
  • 18
  • 31
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
  • 2,039
  • 1
  • 22
  • 25
22

2019:

Get a plugin and you are sorted...

File > Settings > Plugins

enter image description here

Witold Kaczurba
  • 9,845
  • 3
  • 58
  • 67
21

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
  • 18,822
  • 21
  • 92
  • 144
S34N
  • 7,469
  • 6
  • 34
  • 43
  • 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
  • This resolved the issue I was facing, thanks a lot!! – Rakesh Nov 06 '21 at 19:56
19

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
  • 571
  • 6
  • 15
18

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
  • 666
  • 11
  • 20
13

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
  • 9,909
  • 16
  • 60
  • 94
11

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,280
  • 25
  • 34
6

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.

Amrit Kr Lama
  • 109
  • 2
  • 11
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

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

cecile
  • 41
  • 1
4

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

27P
  • 1,183
  • 16
  • 22
4

None of the answers address the fact that you might have incorrect dependencies in your project, if you are using spring boot the dependency definition is easy:

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <!--<version>1.18.12</version> you dont need to specify a version-->
        <type>jar</type>
    </dependency>

But if you are not using spring boot you can import all dependencies:

        <!--region Lombok Configuration -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.8.0-beta2</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <type>jar</type>
        </dependency>
        <!-- endregion -->

This assuming you have the plugin working and the "Enable Annotation Processing" checkbox activated in:

Settings[Ctrl+Alt+S] > Build, Execution, Deployment > Compiler > Annotation Processors

White_King
  • 748
  • 7
  • 21
2

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

Tick on 'enable annotation processing'. Apply

Close

Sumukh Bhandarkar
  • 386
  • 1
  • 5
  • 14
2

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

Example: lombok.log.fieldName = LOG

Taras Melnyk
  • 3,057
  • 3
  • 38
  • 34
2

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,617
  • 2
  • 19
  • 30
2

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
  • 4,160
  • 5
  • 46
  • 82
2

If anyone facing this issue still, it is not because of IntelliJ but you need to specify annotation processor for the maven compiler plugin (of cause if you are using maven)

            <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
      
                            <annotationProcessorPath>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>1.18.12</version>
                            </annotationProcessorPath>

                        </annotationProcessorPaths>
                    </configuration>
             </plugin> 
    ```
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
  • 878
  • 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
  • 508
  • 8
  • 18
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"

1

Use this dependency and try:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.30</version>
</dependency>
Arefe
  • 11,321
  • 18
  • 114
  • 168
1

For me, it works if you are using Maven. I added this in pom.xml of my spring boot project.

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

Then after I restart IntelliJ, you just control+space when typing @Slf4j to auto-import the lib.

and don't forget to enable the Enable Annotation Processing in: Settings > Build, Execution, Deployment > Compiler > Annotation Processors

Yodi S.
  • 106
  • 1
  • 5
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
  • 710
  • 8
  • 10
0

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

Rajiv Singh
  • 958
  • 1
  • 9
  • 14
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.
  • 910
  • 1
  • 14
  • 24
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
  • 20,649
  • 15
  • 100
  • 138
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
  • 327
  • 2
  • 4
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
  • 2,383
  • 12
  • 27
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
  • 352
  • 3
  • 11
0

the compiler javac used was 14 or 17 while the project has the target version 1.8

julien
  • 656
  • 6
  • 9
0

For Neovim with native lsp client and jdtls users who uses the nvim-jdtls plugin.

I also had this issue, running and compiling was working fine but neovim lsp diagnostics keeps reporitng 'symbol not found'. I tried all the solutions mentioned here but it was no use for my case.

Turns out , my issue was lombok was not accessible to neovim lsp, or to be preciese, lombok was not being injected as a JVM arg.

The problem was, in my custom command to launch the jdtls, i had lombok after the jdtls.

java ... -jar /path/to/jdtls/jar -javaagent:/path/to/lombok.jar

Which means lombok was being passed as an argument to jdtls and not as a JVM argument.

To fix that i had to put lombok BEFORE jdtls so it is accepted as a JVM argument.

java ... -javaagent:/path/to/lombok.jar -jar /path/to/jdtls.jar

And that was it. This is a very unique case but do make sure that you have lombok before the jdtls jar.

Just Khaithang
  • 1,217
  • 1
  • 14
  • 24
0

I added the Lombok dependency and it worked for me.

        <!--    Lombok Dependency   -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
LJR
  • 151
  • 10
0

Intellij Idea 2021 - similar error.

Decision:

  • delete an annotation @Slf4j
public class PostgresqlContainerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PostgresqlContainerInitializer.class);

    private static final String POSTGRESQL_IMAGE = "postgres:14-alpine";

    private static final String[] CUSTOM_COMMAND_PARTS = {"-c", "shared_buffers=256MB", "-c", "max_connections=300"};

    private static final PostgreSQLContainer<?> postgresqlContainer = new PostgreSQLContainer<>(
            DockerImageName.parse(POSTGRESQL_IMAGE)
                    .asCompatibleSubstituteFor("postgres")
    ).withDatabaseName("testdb");

....

You only need to declare

   private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PostgresqlContainerInitializer.class);
skyho
  • 1,438
  • 2
  • 20
  • 47