Intellij Cannot resolve symbol on import
Asked Answered
E

34

235

This problem happens intermittently for different libraries and different projects. When trying to import a library, the package will be recognized, but the class name can't be resolved.

If on the import statement, I right-click -> Goto -> the package's declaration, I see all the decompiled classes displayed in the side pane -- Including the ones I need -- If I try to auto-complete the import statement, I notice the class I need is not featured in the dropdown.

I tried invalidating caches already, doesn't work. I cannot find any class conflicts -- there is no other jar file in my classpath with the same package name. I am able to import this class into other projects.

Please see screen shots:

Anyone have a clue?

Emelun answered 15/11, 2014 at 23:39 Comment(5)
Are you sure that the library was added to the module? It could be added as a project library but not be on the modules classpath.Hoarsen
yes -- In Project Structure, the library is added as a dependency -- Actually, I think this is a bug in Intellij -- Happens randomly for different libraries/projects -- Recreating the project from scratch usually fixes itEmelun
I tried invalidating caches and restarting, but the only thing that worked for me was wiping out the .idea directory completely, then creating a new project from scratch.Hap
If everything mentioned in the answer is proper. close the project and re import again. this solved this issue for me.Macrobiotic
In my case the problem was that I had not configured an SDK for the project - after I go to Project Structure and set the appropriate SDK it got resolved.Hood
U
5

I tried every answer here , What fixed my issue was deleting .idea folder , I did invalidate caches and restart before that and a simple restart after deleting .idea , The IDE asked to reimport gradle project , after the import everything started working normally.

Umbrage answered 2/2, 2023 at 4:54 Comment(1)
Worked for me as well. I had duplicated a whole folder (including the .idea directory) to create my project in the first place, and some paths must have been broken or something.Bicentenary
H
506

You can try invalidating the cache and restarting IntelliJ, in many cases it will help.

File -> Invalidate Caches/Restart

Helios answered 16/12, 2014 at 16:1 Comment(9)
cleaning project also helpedGabbi
@Gabbi IMHO, Eclipse cleaning = Intellij Invalidate cacheBouffant
Didn't work for me too. My JUnit was a 'Global Library'. When I remove this and add IntelliJ's own JUnit5 distribution, it works. When I then convert this dependency to a global library again, the errors return. I suspect this is a bug because converting it to a project library instead works fine.Flan
This removed the .iml file in my environment. Re-generating/Re-importing it resolved the issue though.Parke
I have this problem at least once a day since a few weeks. Really stresses me out as re-indexing everything takes forever.Portcullis
This worked for me after opening a project from Spring InitializerChladek
this worked for me, curious why is there such an issue from time to time? whats the cause of it?Frightfully
After "Invalidate Caches/Restart", you have to wait for re-index when you open any project! It's really annoying! I'd rathor delete that file and create it again, wtf!Majormajordomo
‘Invalidate Caches/Restart’ closes all of my Java projects and the IDE will restart. So, if you’ve got more than two projects open, you might want to think twice before doing that.Euchology
N
61

There can be multiple reasons for this. In my case it was wrong source root issue. Invalidate caches didn't work along with other solutions.

Check your module source roots.

  1. Project Structure (Ctrl+Alt+Shift+S).

  2. Modules

  3. Select your problem module.

  4. Change tab on top of window "Sources".

  5. Remove unwanted source roots. Keep one and add src and test source roots in this root.

Neoplasty answered 7/10, 2015 at 17:54 Comment(4)
I do not know if this works for some people, but it seems so, and I wonder the reason why: actually your next Gradle sync will recreate all of those source directories you have just removed.Leonard
It seems that when you have multiple source(src) folders issues arises. This is probably causes by a duplication of paths. In may case I had the main source folder and sub folder within source with additional packages. Ensure that there is only one source folder, and all your sub folder should not be added to source. This will ensure that there is no duplication of file paths. Thanks @inetphantom.Contradictory
In my case, I had Groovy sources in different folders. I had to explicitly add them as as sources to remove "Cannot find symbol" errors on my imports.Cavern
In my case, a target folder was excluded from the sources , this target folder has some files which are generated after compilationCasa
S
44

File -> Invalidate Caches/Restart And Build your project

Scrape answered 13/2, 2018 at 4:14 Comment(0)
S
38

IntelliJ has issues in resolving the dependencies. Try the following:

  1. Right click on pom.xml -> Maven -> Reimport
  2. Again Right click on pom.xml -> Maven -> Generate sources and update folders
Sage answered 2/2, 2018 at 19:24 Comment(3)
There is no "Reimport" after right-clicking -> Maven. There is reload.Singlet
Right click on pom.xml -> Run Maven -> ReimportReferendum
Right click on pom.xml -> Maven -> Reload project worked for me..!!Knockabout
H
31

Run this command in your project console:

mvn idea:idea

Done. Had this issue many times. Tried 'Invalidate Cache & Restart' and all other solutions. Running that command works perfect to me. I'm currently using IntelliJ 2019.2, but this also happened in previous versions and solution worked as well.

Hewie answered 11/9, 2019 at 16:12 Comment(3)
Btw this maven plugin is no longer maintainedMesozoic
What if we're using gradle ?Volar
Some further info on the command is available here: maven.apache.org/plugins/maven-idea-plugin/usage.htmlSmother
O
18

File -> Invalidate Caches/Restart or rebuilding the project did not work wor me.

What worked for my Gradle project was to "Refresh all Gradle projects" from the Gradle tab on top-right corner of IntelliJ v2017, using the yellow marked button shown below:

enter image description here

Overcapitalize answered 25/6, 2018 at 13:20 Comment(2)
This answer did the job for my Maven project also. For IntelliJ 2018.3.4 the icon stays the same (blue turning arrows) but the exact title of the option is called 'Reimport All Maven Projects'. Huge big up to the author, it is so relieving to find a correct solution for your problem after a one hour researchInsufficient
For me, it's a combination of invalidating caches, restarting, and then refreshing all Gradle projects.Jesseniajessey
P
14

Check your module dependencies.

  1. Project Structure (Ctrl+Alt+Shift+S).
  2. Modules
  3. Select your problem module.
  4. Change tab on top of window "Dependencies".
  5. Check what needed library (maybe, you need to add specified library in the tab 'libraries') or module has listed here and it has right scope ('complile' mostly).
Peshawar answered 3/9, 2015 at 9:18 Comment(0)
C
12

I faced a similar issue, Mike's comment helped me move in the direction to solve it.
Though the required library was a part of the module in my project too, it needed a change of scope. In the module dependency, I changed the scope to "Compile" rather than "Test" and it works fine for me now.

Cystotomy answered 30/4, 2015 at 10:50 Comment(0)
H
7

Had the same problem till I noticed that the src folder was marked as root source instead of java! Changing to only the java (src/main/java) to be the source root solved my problem enter image description here

Habitforming answered 11/7, 2018 at 22:31 Comment(0)
G
5

I found the following answer from @jossef-harush and @matt-leidholm useful from another link

  • in IntelliJ editor, click on the red keyword (Integer for example) and press ALT + ENTER (or click the light bulb icon)
  • select Setup JDK from the intentions menu

IntelliJ intentions menu

  • click on Configure

Project SDK selection dialog

  • In my case, the JDK path was incorrect (pointed on /opt/jdk1.7.0_51 instead of /opt/jdk1.7.0_65)

Broken Configure SDK dialog

  • click on the ... and browse to the right JDK path

Fixed Configure SDK dialog

  • let's clear the cache

IntelliJ File menu

Gauze answered 28/10, 2017 at 17:40 Comment(0)
U
5

I tried every answer here , What fixed my issue was deleting .idea folder , I did invalidate caches and restart before that and a simple restart after deleting .idea , The IDE asked to reimport gradle project , after the import everything started working normally.

Umbrage answered 2/2, 2023 at 4:54 Comment(1)
Worked for me as well. I had duplicated a whole folder (including the .idea directory) to create my project in the first place, and some paths must have been broken or something.Bicentenary
T
3

After a long search, I discovered that a dependency was somehow corrupted on my machine in a maven project. The strange thing was that the dependency was still working correctly in the compiled java code. When I cleaned and rebuilt my maven dependency cache however, the problem went away and IntelliJ recognized the package. You can do this by running:

mvn dependency:purge-local-repository

Intrestingly, the source of my problem hence wasn't IntelliJ, but maven itself.

Tina answered 14/2, 2019 at 13:46 Comment(0)
M
2

Right click on pom.xml file, go to Maven click on Reimport. I had similar problem and this worked for me.

Matthus answered 18/9, 2018 at 5:35 Comment(0)
Q
2

For 2020.1.4 Ultimate edition, I had to do the following

View -> Maven -> Generate Sources and Update Folders For all Projects

The issue for me was the libraries were not getting populated with mvn -U clean install from the terminal.

enter image description here

Quinary answered 25/7, 2020 at 2:29 Comment(0)
S
2

Try cleaning maven from upstream by:

mvn -U clean install
Shishko answered 27/7, 2021 at 12:56 Comment(0)
C
1

I also got this error for multiple times when I try to build a new java project.

Below is the step how I got this stupid issue.

  1. Create an empty project, and create new directory src/main/java.
  2. Create the source package net.gongmingqm10.sketch.
  3. Use gradle wrapper, gradle idea to build the gradle stuff for the project.
  4. Add some dependencies in build.gradle file, and gradle build, reimport the project.
  5. Create User.java and School.java in the existing package net.gongmingqm10.sketch
  6. I got the error while I try to use import School in User.java.
  7. Intellij keeps complain can not resolve symbol on import.

Solution:

Build the project first, and mark the main/java as the source root. Create a new directory with the same name net.gongmingqm10.sketch. Move all the files from the old troubling packages to new package.

Root cause:

Directory tree of source code

As you can see from the tree. I got a directory named net.gongmingqm10.sketch. But what we really need is the 3 levels directory: net->gongmingqm10->sketch

But before I finish building my project in Intellij, I create new directory named net.gongmingqm19.sketch, which will give me a real directory with the name net.gongmingqm10.sketch.

When we trying to import it. Obviously, how can intellij import the file under the weired directory with the name a.b.c.

Carbarn answered 19/8, 2016 at 17:59 Comment(1)
Thanks dude, this was my problem. edit: elaborating... I copied from 1 maven module to another, and the copy flattened the directories. while Intellij looked like it had everything sorted, my folder was in fact "a.b.c" rather than a>b>c.Alack
E
1

@Anton Dozortsev I was driven crazy by a similar behavior; I ended up re-installing the IDE a bunch of times, only getting past the issue after removing the IDEA app, all caches, preferences, etc.

I tried all kinds of steps in the interim, (like restarts, invalidations, deleting .idea and *.iml files, and others.)

Turns out, the problem was due to IntelliJ's idea.max.intellisense.filesize setting. I had set it to 100KB, which was smaller than my dependency's size, leading to IntelliJ showing it as unknown, just like in your screenshot.

Fix:

  1. Click on Help -> Edit Custom Properties

  2. Set the property to a larger value; the default is 2500KB idea.max.intellisense.filesize=2500

Eyeopening answered 12/6, 2017 at 17:9 Comment(0)
C
1

Please try File-> Synchronize. Then close and reopen IntelliJ before you invalidate.

Once I restarted. I would have invalidated but the synchronize cleared everything after restarting.

Cogitative answered 30/7, 2019 at 15:28 Comment(0)
E
1

Nothing I tried above worked for me (not that I tried every suggestion). What finally did the trick was to rename the class -- I just added a 2 to the class name and filename. Then I resolved all the references manually. (Since they weren't recognized, the refactoring did not change the references automatically.)

Once the "2-version" was happily resolved everywhere, I was then able to refactor and remove the 2 from the class and file, and everything was then as it should be.

Elizbeth answered 24/12, 2019 at 21:59 Comment(0)
P
1

Faced similar issue, I Updated Intellij and error start coming - Can't Resolve Symbols.

Went to Plugins, Updated the plugins & Restart Problem Solved !!

Protolithic answered 13/4, 2022 at 15:5 Comment(1)
for me, I only need to restart IDE.Castoff
H
0

Simple Restart worked for me.

I would suggest first try with restart and then you may opt for invalidating the cache.

PS : Cleaning out the system caches will result in clearing the local history.

Hebe answered 9/12, 2016 at 23:2 Comment(0)
T
0

I found the source cause!

In my case, I add a jar file include some java source file, but I think the java source is bad, in Intellij Idea dependency library it add the source automatic, so in Editor the import is BAD, JUST remove the source code in "Project Structure" -> "Library", it works for me.

Terranceterrane answered 23/4, 2017 at 0:2 Comment(0)
G
0

What did it for me is to edit the package file in the .idea folder as I accidentally added sources to this jar library and android couldn't resolve it by deleting the sources line as marked in the b/m picture library error.

Then rebuild the gradle and bam problem solved.

Gibe answered 1/5, 2017 at 17:6 Comment(0)
U
0

I had the same issue and the reason for that was incorrect marking of the project's sources.

I manually created the Root Content and didn't notice that src/main/test folder was marked as Sources instead of Tests. So that is why my test classes were assumed to have all their test libraries (JUnit, Mockito, etc.) with the scope of Compile, not Test.

As soon as I marked src/main/test as Tests and rebuilt the module all errors were gone.

Us answered 5/6, 2017 at 12:40 Comment(0)
D
0

I had this recently while trying to use Intellij to work on NiFi, turned out the issue was that NiFi requires Maven >= 3.1.0 and the version that I'd checked out with (I guess my default) was 3.0.5. Updating the Maven version for the project fixed it, so in some cases Maven version mis-alignment can be a thing to look...I'd guess it's fairly unusual but if you get this far on the thread you're probably having an unusual issue :)

Depress answered 3/1, 2018 at 18:54 Comment(0)
E
0

file-> Project Structure -> Modules, find the module with problems, click it and choose the Dependencies tab in the right side. Click the green plus sign, try to add the jar or libraries that cause the problem. That works for me.

Evocative answered 25/10, 2018 at 11:58 Comment(0)
D
0

in my case the solution was to add the project as maven project, besides the fact that i imported as maven project :P

go to pom.xml -> right click -> add as maven project

Depalma answered 29/1, 2020 at 10:42 Comment(0)
A
0

My issue was my Maven plugin got disabled after an update. I went to Help -> Find Action... -> Typed in Maven and found that it was "Off". I clicked the toggle switch and after a bit of loading it was re-enabled.

Ailurophile answered 18/5, 2021 at 15:32 Comment(0)
C
0

Also, check your class is not in compile exclusions

If you see, that there is a little grey cross in left top corner, you must remove class from compile exclusions

enter image description here

How to remove

enter image description here

enter image description here

Crankshaft answered 26/5, 2021 at 13:29 Comment(0)
M
0

Old question, '21 response. I ran into the issue where my go build would build code successfully but my Goland IDE showed missing modules or dependencies. I tried Invalidating Caches and Restart, but had the same problem. From another S/O thread, I tried adding the GO111MODULE=on to my Path Variables, but that didn't resolve the IDE problems either.

What worked for me was picking the correct GOROOT path in Preferences > Go > GOROOT.

I had two versions of go installed, one by brew and one from the online Go installer. I selected the brew install path, and my IDE was able to resolve the dependencies properly.

Madelaine answered 25/10, 2021 at 20:24 Comment(0)
O
0

I've tried all the complicated methods and they didn't work, since I was too lazy to re-import the project I tried something else. Mine is a gradle project, so I went to my gradle.build file, removed the dependency, refreshed the dependencies, then added the dependency again and refreshed again, the imports started working after that.

Osmund answered 24/1, 2022 at 13:11 Comment(0)
S
0

I don't know what causes the issue. I tried everything suggested here: deleting build directory, .iml file, invalidating cache and restarting but nothing worked. IntelliJ was suggesting to update the JDK corretto-17 to Amazon Corretto-17.0.9 which fixed it.

Sabra answered 27/12, 2023 at 13:58 Comment(0)
P
0

In my case, simply, I had not placed my code under path src\main\java. I had had it simply under src. When I created the src\main\java path and moved my code under that, the problem went away.

Panjabi answered 8/1 at 16:27 Comment(0)
H
-1

I had a similar issue with my imported Maven project. In one module, it cannot resolve symbol on import for part of the other module (yes, part of that module can be resolved).

I changed "Maven home directory" to a newer version solved my issue.

Update: Good for 1 hour, back to broken status...

Hustler answered 11/1, 2017 at 22:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.