Building with Lombok's @Slf4j and Intellij: Cannot find symbol log
Intellij IdeaSlf4jLombokIntellij Idea Problem Overview
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?
Intellij Idea Solutions
Solution 1 - Intellij Idea
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
Solution 2 - Intellij Idea
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?
Solution 3 - Intellij Idea
In Intellij version 2016, 2017, enable Preferences -> Compiler -> Annotation Processors does not work for me!
Solution 4 - Intellij Idea
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
Step 2. Install lombok plugin in IntelliJ IDE after that restart IDE. Screenshot
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'
hope this answer is helpful for you.
Solution 5 - Intellij Idea
Solution 6 - Intellij Idea
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'
}
Solution 7 - Intellij Idea
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
Solution 8 - Intellij Idea
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'
Solution 9 - Intellij Idea
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>
Solution 10 - Intellij Idea
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
andProcessor 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
Solution 11 - Intellij Idea
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.
Solution 12 - Intellij Idea
In IDEA 13 this seems to no longer be an issue, you just have to have the Lombok plugin installed.
Solution 13 - Intellij Idea
I've just installed the latest idea verion 2108.1 and found this issue, after installed lombok plugin and restart the Idea resolve it.
Solution 14 - Intellij Idea
Removing the @Slf4J annotation from the class and then re-adding it worked for me.
Solution 15 - Intellij Idea
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
Solution 16 - Intellij Idea
This worked for me :
File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor
Tick on 'enable annotation processing'.
Apply
Close
Solution 17 - Intellij Idea
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.
Solution 18 - Intellij Idea
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.
Solution 19 - Intellij Idea
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!");
}
}
Solution 20 - Intellij Idea
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"
Solution 21 - Intellij Idea
IDEA 2021 and still have the issue, to solve it make sure that you have the last version of lombok in your project dependencies.
Solution 22 - Intellij Idea
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
Solution 23 - Intellij Idea
Delete .idea folder and .iml files in each module and rebuild the solution.
Solution 24 - Intellij Idea
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.
Solution 25 - Intellij Idea
A simple thing but I figured it out is: I missed adding @Log
to the class.
@Log
public class YourClassName {
}
It may help someone.
Solution 26 - Intellij Idea
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.
Solution 27 - Intellij Idea
Try to create lombok.config
file under project base directory and provide lombok.log.fieldName
value.
Example: lombok.log.fieldName = LOG
Solution 28 - Intellij Idea
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
.
Solution 29 - Intellij Idea
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.
Solution 30 - Intellij Idea
Use this dependency and try:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
Solution 31 - Intellij Idea
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
Solution 32 - Intellij Idea
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>
```
Solution 33 - Intellij Idea
the compiler javac used was 14 or 17 while the project has the target version 1.8
Solution 34 - Intellij Idea
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