I’m fairly new to Intellij and Java and am unable to use libraries from one "module" in another. I am seeing red class names and getting "Cannot resolve symbol . " errors all over the place:
The other modules are referenced in File | Project Structure all the other module’s Paths are correct and include the *.classes I’m trying to use:
I have also tried a File | Invalidate Caches but am still having the same problems.
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?
26 Answers 26
you can try invalidating the cache and restarting intellij, in many cases it will help.
File -> Invalidate Caches/Restart
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.
Project Structure (Ctrl+Alt+Shift+S).
Select your problem module.
Change tab on top of window "Sources".
Remove unwanted source roots. Keep one and add src and test source roots in this root.
Check your module dependencies.
- Project Structure ( Ctrl + Alt + Shift + S ).
- Select your problem module.
- Change tab on top of window "Dependencies".
- 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).
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.
IntelliJ has issues in resolving the dependencies. Try the following:
- Right click on pom.xml -> Maven -> Reimport
- Again Right click on pom.xml -> Maven -> Generate sources and update folders
Run this command in your project console:
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.
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:
File -> Invalidate Caches/Restart And Build your project
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
- In my case, the JDK path was incorrect (pointed on /opt/jdk1.7.0_51 instead of /opt/jdk1.7.0_65 )
- click on the . and browse to the right JDK path
Right click on pom.xml file, go to Maven click on Reimport. I had similar problem and this worked for me.
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.
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.
I’ve run into a bizarre problem where IDEA tells me that it cannot resolve symbol for classes within the same package as the class open in the editor. And, if I use Alt-Enter to add an import line, the import line shows up as greyed-out for ‘unused import statement’ (but the ‘cannot resolve symbol’ flag goes away). Similarly, there are a number of classes being flagged with cannot resolve symbol while there’s a greyed-out import line that contains a wild-card import that includes those flagged classes.
This is in IDEA 9.0.4 and only just started happening.
Invalidating the caches did not fix the problem
Deleteing the cache files in the filesystem did not fix the problem
Deleting the module and rebuilding it did not fix the problem
Any other ideas?
Which languages are these classes written in? Are they located under a source root? Are they in the same module? Do they have correct package statements? Is the same problem present in IDEA X?
These are Java classes.
They live in a subdirectory of a directory marked source in the module settings (which I what I’m guessing you mean by source root — and I checked to make sure I hadn’t accidentally excluded that subdirectory)
They are in the same module
They have correct package statements
I have not tried this in IDEA X as getting work done is more pressing right now
This is a problem I’ve never seen in IDEA before (and I’ve been using it since 5 or 6)
Some classes from within the same module are imported without problem
However, when I changed one of those imports to a wild card, the Auto Import feature added a FQN import and the wild card import ended up greyed-out as another ‘unused import statement’
Do you have multiple classes with the same short name (in different packages)?
Nope — no short name collisions
Could you please attach your logs (see http://devnet.jetbrains.net/docs/DOC-181) and some screenshots of the errors? Does this happen in one particular project or in all? It would be great if you could provide a project with this problem for us to reproduce the problem.
Here’s a zip of the log directory — the last things I did was to open a file demonstrating the problem and then close IDEA
On providing a project — would sending you the .idea project dir and module .iml files help without having the relevant source code?
I see you’re using some 3d party plugins. Could you please try disabling them (in particular FindBugs) and see if the problem persists. If yes, then please attach the *.iml files.
I’ve emailed those iml files to you.
Thank you for the persistent followup
Were you able to solve this problem? I have the same trouble.
Not yet. I need some sample project to be able to fix it.
Did you kill IDEA through the task manager? I’ve seen this problem, especially in IDEA 10, after I have had to kill the process.
I started seeing the problem after my computer powered off spontaneously while IDEA was updating the project (the power off had nothing to do w IDEA, faulty laptop docking station) — my project got corrupted and I had to rebuild it. Ever since then, I’ve had this problem.
Afa providing a project, I’m not sure I could repro the problem and the project that has the problem contains proprietary code.
Are you certain that File | Invalidate Caches doesn’t help?
I started seeing the problem after my computer powered off
spontaneously while IDEA was updating the project (the power off had
nothing to do w IDEA, faulty laptop docking station) — my project got
corrupted and I had to rebuild it. Ever since then, I’ve had this
Afa providing a project, I’m not sure I could repro the problem and
the project that has the problem contains proprietary code.
"Develop with Pleasure!"
Yes, and I’ve also tried removing the cache files directly in the file system
Could it be that your project has a library whose classes or source roots point to somewhere inside the project (e.g. another module)?
I know this is a little late, and hopefully you already found a solution, but I’ll post this any way as I just ran into this exact problem and found a solution (that at least worked for me). Hopefully this will save someone else an afternoon of frustration.
I had the exact same symptoms as Andy, nearly suddenly certain classes could not be found, even though the files were obviously there. For many of the cases, the classes were actually in the same package. I noticed that while the parent directories were all listed as sources, certain, nearly random subdirectories were not.
Like Andy, I removed the module from my project, invalidated the caches, physically deleted the cache files, restared IntelliJ, and then re-added the module. Unfortunately, this did not resolve the problem. I found that if I also removed the index and localhistory files, then restarted IntelliJ and re-added the module, that made IntelliJ rebuild everything and all my packages could be found.
Even this is very late I will share it with you.
I got the same problem, but also with methods in super classes.
My project compiles and runs even I can’t see everything in the JDE.
It started with v10 community, I then went to v10 Ultimate, still same problem, then v11 ultimate no change.
Opening the same project in community v9 everything is as expected, therefore I suspect v10 and above to have a bug.
I have tried to invalidate, clear out files etc. no luck.
Please bear with me, I’m pretty new to this — so my explanition migth not be to clear.
I then looked at the hierachrcy and it appears it happens when a class extend another class not in the project, but taken from a jar. All classes are in the same package.
class C extends Class B extends class A
Class C has a problem seing methods and symbols in class A, as class B is taken from a jar, then class A is also taken from that jar, despite A exist in the prjoect.
class E extends Class D extends class A
Here we have no problem seing what’s in A, as class D is in the project.
So to me it looks like the resolver gets side tracked if it has to go to a jar and then can’t get back into the project.
This might not be the right place to put it — please guide if this is the case.