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?
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
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?
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
In IDEA 13 this seems to no longer be an issue, you just have to have the Lombok plugin installed.
I've just installed the latest idea verion 2108.1 and found this issue, after installed lombok plugin and restart the Idea resolve it.
This worked for me :
File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processor
Tick on 'enable annotation processing'.
Apply
Close
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.
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!");
}
}
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"
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
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
testCompile 'org.projectlombok:lombok'
testImplementation 'org.projectlombok:lombok'
}
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
来源:https://stackoverflow.com/questions/14866765/building-with-lomboks-slf4j-and-intellij-cannot-find-symbol-log

