2

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:
  • Java 11.0.6
  • Latest versions of Maven and IntelliJ
  • macOS Mojave

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.

2 Answers2

1

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

z atef
  • 7,138
  • 3
  • 55
  • 50
  • If you look more closely at my post, you’ll see that I already tried re-importing the project. I even cloned the repo again and imported it into a new IntelliJ project, which is at least equivalent to deleting the .idea directory. I did check the jars just now, and all the ones in question are there. I tried all the obvious things multiple times this week, and that’s why I’m posting my question here. It seems like something really obscure is causing the problem. – Johnny Rocketfingers Aug 01 '20 at 03:14
  • Does import org.springframework.http; (on the import section) look okay? Also the version specifying. – z atef Aug 01 '20 at 03:17
  • Yes. I am certain that the import statements and the pom.xml are in good shape. I’m taking the code straight from the master branch of our repos, and it works on everyone’s machine but mine. I’ll reiterate that projects all install and run flawlessly on my machine in both terminal and IntelliJ. It’s just that the editor doesn’t want to cooperate. It’s flagging errors that aren’t actually there. – Johnny Rocketfingers Aug 01 '20 at 04:25
  • 1
    Hmm, how about File -> invalidate Cache /Restart – z atef Aug 01 '20 at 05:57
  • Once again, it’s on the long list of stuff I’ve already tried. I wrote it all in the original post. – Johnny Rocketfingers Aug 01 '20 at 08:31
  • If you have done all these with no good result, post the question in IntelliJ Q&A forum.. :) https://youtrack.jetbrains.com/issues/IDEA – z atef Aug 03 '20 at 03:57
0

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.

fauzimh
  • 594
  • 4
  • 16
  • Good suggestion, but unfortunately that is not the issue here. I looked into it earlier in the week and just now double checked. I have no “exclude patterns” set. I think I’ve possibly tried every single solution on google by now. I’m at the point where I am considering just reinstalling IntelliJ, and I’m not certain if even that will work. – Johnny Rocketfingers Aug 01 '20 at 04:20