Johnny Rocketfingers
Johnny Rocketfingers

Reputation: 75

IntelliJ does not recognize certain Spring packages

In my rest API projects, IntelliJ suddenly stopped recognizing imports from the packages org.springframework.http and org.springframework.web. For example, it gives me the red squiggleys when I try to create an instance of ResponseEntity or use the @RestController annotation. I also get the red squiggleys under the corresponding import statements with the message “Cannot resolve symbol” when I hover my cursor over them.

I click the light bulb and select “add Maven dependency”, but IntelliJ cannot find any to add. Even if it could, the proper Maven dependencies are already present in the pom.xml, but they are grayed-out as if they aren’t referenced anywhere. Manually specifying different versions of the dependencies does nothing. Reimporting the project does nothing. Cloning the repo again and starting a new project didn’t help. Neither did invalidating the caches, deleting the entire .m2 directory, updating IntelliJ, restarting my machine, or fiddling with the interpreter.

The code builds and runs as expected without any problems both in terminal and in the IDE. I have no idea why the IntelliJ seems so confused. This is only happening on my machine, and I can’t think of anything I could have done to cause it. Nobody else on the team has seen this issue. It’s not isolated to any particular repo. It happens any time I try to import from these specific packages. It’s been bugging me all week, and my teammates are just as bewildered as I am. Any suggestions?

Additional Info:

Update

I decided to once again delete my entire .m2 directory and the .idea directory for my project. I have done this multiple times over the past week with no results. This time, when I reimported the project I found the aforementioned org.springframework.web and org.springframework.http imports are now being recognized correctly. However, IntelliJ is now failing to recognize some of the other imports, such as lombok.

I actually had this particular version of the problem last week for several hours. I then took a short break and opened my laptop to find that everything was magically back to normal. A few days later, I started having the error that I described above. These unrecognized imports seem to appear and disappear at random, regardless of the project I am working in or any changes to the code.

Let me reiterate that there is nothing wrong with the code. IntelliJ builds and runs it flawlessly, even with all the red squiggleys. There is something wonky going on with the editor. I have included a screenshot below of the current state.

IntelliJ says "Cannot resolve symbol"

Solved

I decided to uninstall and reinstall IntelliJ. which resolved the issue.

Upvotes: 3

Views: 3437

Answers (2)

fauzimh
fauzimh

Reputation: 640

Refer to the image link here Exclude from import and Completion

In Intellij settings File-> Settings, go to Editor -> General -> Auto Import -> Java section -> Exclude from import and completion. Remove those classes you have accidentally put there. It might have ever happened coz you have ever pressed the wrong button when there is an auto-suggest from Intellij.

Actually, I have the same problem a few days ago. And that is how I resolved it.

Upvotes: 0

z atef
z atef

Reputation: 7679

This can happen for lots of reasons:

Try the following:

(Mac users) Right click on the project -> Maven -> reimport

If that doesn't work:

Delete `.idea` folder and re import the project.
Its located in the root of the project. like this one

za$ ls .idea/
.name          compiler.xml   encodings.xml  httpRequests/  misc.xml       vcs.xml        workspace.xml  

If that doesn't work, make sure the jar is loaded and you can see it listed as a dependency, or import it manually. see this questions:

https://stackoverflow.com/questions/1051640/correct-way-to-add-external-jars-lib-jar-to-an-intellij-idea-project

If that doesn't work:

Delete all versions of the jar(that having the issue) from ./m2 folder. Or make sure to specify a specific version in pom.xml

Upvotes: 2

Related Questions