IntelliJ inspection gives "Cannot resolve symbol" but still compiles code
Asked Answered
W

65

779

Platform: IntelliJ Community Edition 10.0.3
SDK: jdk1.6.0_21
OS: Windows 7

So I have a strange situation with IntelliJ that has me completely stumped. I setup a Maven project and add log4j as a dependency in the pom.xml file. The IDEA inspections run fine and my unit tests all compile and run.

I then added hunnysoft's jmime library to my local maven repository using the mvn install:install-file as follows.

mvn install:install-file -Dfile=jmime.jar -DgroupId=jmime \
-DartifactId=jmime -Dversion=3.1.1e -Dpackaging=jar

Maven installed the jar file just fine into my local repository.

I then went into IntelliJ's Settings => Maven => Repository Services and Updated my local repository (so that IntelliJ would reindex the repository contents).

Finally, I added the following dependency to my pom.xml file (just above the log4j dependency).

<dependency>
    <groupId>jmime</groupId>
    <artifactId>jmime</artifactId>
    <version>3.1.1e</version>
</dependency>

I now create a new class as follows:

package com.stackoverflow.question;

import org.apache.log4j.Logger;
import com.hunnysoft.jmime.ByteString;
import com.hunnysoft.jmime.Field;
import com.hunnysoft.jmime.FieldBody;

public class StackOverflowQuestion {
    public Field create(String name, String text) {
        Logger.getLogger(getClass()).debug("create entered");
        FieldBody body = new FieldBody();
        body.setText(new ByteString(text));
        Field field = new Field();
        field.setFieldName(name);
        field.setFieldBody(body);
        return field;
    }
}

Now for the weirdness. IntelliJ's intention mechanism picks up and recognizes the Logger import in the maven pom file just fine. However, for all of the hunnysoft imports it reports: "Cannot resolve symbol 'ByteString/Field/FieldBody'", BUT Build => Compile 'StackOverflowQuestion.java' compiles everything correctly and the unit test I created for this class runs fine (though the intentions mark the call to create() as a problem area too).

So somewhere, somehow IntelliJ is ignoring the jmime.jar file for the intention subsystem. I'm confused because the log4j dependency works fine and everything compiles and runs fine. F12 ("Go To Declaration") works on the Logger import, but breaks on all the jmime imports.

Oh, one other thing, if I go to the 'Packages' view in the "Projects" window the "com.hunnysoft.jmime" package appears and I can see ALL of the classes I imported in the code snippet above under "Libraries". Removing the above dependency from the pom.xml file causes this package to disappear and the compilation breaks.

It appears that the inspection's classpath is broken, but there does not seem to be a setting for this anywhere in the Settings => Intentions | Compiler areas (not that I expected any such settings, I believe the IDEA should already know the correct classpath based on the pom file and JDK).

As a final experiment I created a brand new standard J2SE application project (without using maven) and added the jmime.jar file directly to the project as one of its libraries. I run into exactly the same problems as described above in this new project.

Here is the MANIFEST.MF from the jmime jar file.

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.5.4
Created-By: 10.0-b23 (Sun Microsystems Inc.)

Name: com/hunnysoft/jmime/
Sealed: true
Specification-Title: Hunny JMIME
Specification-Version: 3.1.1
Specification-Vendor: Hunny Software, Inc.
Implementation-Title: com.hunnysoft.jmime
Implementation-Version: 3.1.1E
Implementation-Vendor: Hunny Software, Inc.

I don't see anything unusual in this jar file.

My best guess is that perhaps the problem might be a missing dependency issue. But AFAIK jmime is supposed to be self contained (JarAnalyzer doesn't come up with anything, but I'm not sure it would if a dependency jar is missing).

So, anyone have any IDEAs?

Wardrobe answered 6/5, 2011 at 1:24 Comment(6)
See also #15047264 if it cannot resolve symbols that are part of maven dependencies [for me it was a missing M2_HOME]Superscription
2018.3.3. Still the same error...Helsinki
File>Invalidate Caches/Restart worked for me in version 2020.03.Allyn
This has been [mostly] duplicated (although duplicate has a lot of similar good Answers by now) - with slight difference to here, of specifying prior-successful mvn clean installButene
For IntelliJ IDEA 2022.3.1 Version (Ultimate Edition), my fix is to direct to Settings > Build, Execution, Deployment > Build Tools > Maven > Importing > Uncheck the option "Import using the new IntelliJ Workspace Model API (experimental)", after that reload all project in Maven tab of IntelliJAscites
I deleted the build/ folder under the source code project and trigged another build, and it works!Carmelitacarmelite
M
1337

First of all you should try File | Invalidate Caches and if it doesn't help, delete IDEA system directory. Then re-import the Maven project and see if it helps.

In some weird cases compiled classes may report wrong info and confuse IDEA. Verify that the classes from this jar report correct names using javap.

Monopode answered 6/5, 2011 at 1:31 Comment(35)
I deleted project files, dirs, but it wasn't until the invalidate cache and restart fixed it. Thanks for the tip! I wonder if there's not some way this should be done automatically. You would think so. I mean, I even totally reimported the project from pom.xml, and it still hadn't "invalidated" it's caches.Habanera
FWIW, these fixes did not work for me. I have a maven project that has a dependency on another project. Builds work inside and outside of IDEA 10.5 but some of the symbols from the other project are marked red in the dependent project. Invalidated cache and restarted. No luck. Also removed IDEA files and re-imported the dependent project. No luck.Amperehour
@pastafarian, that might be a known bug that is already resolved, did you try IDEA 12?Monopode
Thanks for the response. It turned out to be, finally, that I had to tell IDEA to "import my Maven project using Maven 3". Under Settings->Maven->Importing. Discovered after a long email exchange with [email protected].Amperehour
@Monopode Well then I thank you again :-) Also this might be an opportunity for IDEA to "guess" that if I have configured IDEA to use Maven3 ("mvn -version") that I may want to import with Maven3.Amperehour
Hurray! I similarly couldn't get IDEA to recognize an import after creating dependencies. All it took was invalidating caches to fix :)Millymilman
I had the same problem after using Intellij to refactor some methods into a separate class. Invalidating caches also solved that issue.Tye
for me this only works till the IDE finishes the indexing. after that no other class is recognizedBarbrabarbuda
@Barbrabarbuda please contact our support for help: intellij-support.jetbrains.com/homeMonopode
The menu option is now "File | Invalidate Caches/Restart" (Android Studio 0.8.2, linux).Staves
I had a similar problem that this fixed. Referenced Groovy classes in the same package that weren't explicitly imported (they needn't be) were showing up red in the editor. Blowing the cache fixed this.Shulins
@Monopode Thanks for this answer, can I just ask why this works as it does?Unwholesome
invalidate didn't work no matter how many times i tried. I deleted the .idea folder and then re-imported the project with build.gradle. It works now.Conditional
Invalidate Caches did not work nonetheless, deleting system folder did the work...Betulaceous
Just had the problem too, and I changed the Maven home directory from the one bundled into Intellij (Maven3) to my own install (3.2.5), and this worked. All my dependencies were found again. Hope it helps...it seems the bundled version has some issues, not sure why.Castleman
The link for "delete IDEA system directory" is broken. Not sure if this page is the one you wanted.Nickolenicks
@Monopode I just installed intelliJ the other day. This issue of "cannot resolve Symbol" has happened 10 times since then. I invalidate caches, restart, it comes back 10 minutes later. This is broken. 2016.2.4Calia
@Calia please submit a bug report at youtrack.jetbrains.com/issues/IDEA with a sample project to reproduce and the logs.Monopode
Yes! File > Invalidate Caches fixed it. Using IntelliJ 2016.3 on OS X. Thanks!Meeting
Hi @Monopode i am Having same error and i also i tried all the solution. What to do now. Plaese Help. what i am doing is . 1 i am importing an eclipse project to IDEA and i am having same problem rather i am having all the jar file for GooglePaly and Google adView which i kept inside the myProject/bin/dexedLibs. and i am having the error "cannot resolve symbol google"Lyonnaise
I tried all of this with no joy. I had to delete the project and re-clone from git. This solved it.Cherriecherrita
Indeed, solved my problem! Just curious, what could lead to such an issue? The project I'm working on is built with Gradle. Clean, rebuild - nothing worked - partially code has been marked as red due to symbol resolving issue, however still perfectly compiled...Arterialize
This bug still existing in Oct. 2017. Spent 1 hour debugging, any ETA to fix please @MonopodeSelfaddressed
Thanks! It solved another my problem. Css autocompletion didn't work in jsp filesBorsch
What wasn't immediately obvious to me but ended up working was that the "delete IDEA system directory" step on OS X involves deleting all of the 4 directories listed for OS X in the aforementioned link. note this will reset everything about IDEA, essentially functioning as a fresh install.Dvinsk
File | Invalidate Caches/Restart works for me in Mac OS X.Sacrilege
For me it worked a mix of solutions, with the following steps (I don't know if all the steps are needed): File | Invalidate Caches and Restart, right click on the code editor, hover on Maven and expand, click on Reimport, and then again File | Invalidate Caches and RestartAbsorbent
Need to make sure the required plug-ins are installed as well. For my case I realised that the Lombok plugin was missing hence there was Cannot resolve symbol warnings here and there. The project uses Lombok for convenience..Thickleaf
Deleting the .iml file and re-importing the project worked for me !!Eberta
this annoying issue happens with this Idea and Android Studio as well. Those useless invalidate/sync do nothing!Haimes
Tried several times - Invalidate cache and restart did not work. The only solution that worked is to delete .idea folder and re importing the modules. I know re importing the modules is pain if you have so many like mine but better than stuck with the same error message.Packaging
If "invalidate cache" doesn't work, don't just go and delete the system configuration directory straight away. Delete the .idea file first in the base of your project then reimport your project instead. That will create a new system configuration directory for your project. Also ensure that the project has a Java SDK defined by going to Project structure -> Project -> Project SDK.Prussiate
Deleting the system folder fixed the issue for me. It was the only thing I hadn't previously tried. Even re-installing IntelliJ didn't fix it.Whipstitch
What's the difference between this and reload project?Heist
Worked for me , FYI if you have more than 1 java version running in local (1.8,11 etc) make sure to select one that you need before the indexing happens.you can do that by IDE and project settings -> project Structure (ctrl+alt+shift+s)Mulvaney
G
142

The following trick resolved this issue for me:

  • Right click on the code editor
  • Hover on Maven and expand
  • Click on Reimport

My idea version is 12.0.4

Goodhen answered 9/4, 2013 at 5:21 Comment(7)
How do i do this in IntelliJ for Mac OS ?Eutherian
@AnkitRustagi for Mac OS right click project directory under project panel. Maven -> Reimport. You can also use the hotkey, Double tap shift, or Shift + Command + A, and search for "reimport all maven projects"Sisely
Right click is a hint for me. Apparently my pom.xml does not marked as maven file, thus I need to right click and marked is as Maven. I have enabled 'Import Maven projects automatically' and all the packages imported after that.Sweettalk
for 2017.1.x is working reimport maven projects: View -> Tool Windows -> Maven Projects, then click on cycle arrows icon.Clegg
Reimporting the Maven module helped me too, I was surprised it isn't reimported as dependencies change anyway...Febrifuge
What "Maven"? I don't see any maven in the right click menu. If it is the one in the project directory (win10) this did not help meHervey
When I cloned a maven project from github I also got the the same problem, Sidebar > Reload All Maven Projects(the circular refresh icon on top) solved the problem for meEtom
S
49

None of the solutions above worked for me. What did was removing the main.iml file manually and it suddenly worked.

Secundine answered 1/9, 2016 at 12:3 Comment(6)
Big thank you! This also was the only solution that solved the issue I had. What happened was my project was renamed and IntelliJ somehow didn't clear out the old .iml files under .idea/modules. Delete those extra .iml file and rebuild the project fixed the issue.Lenin
Worked! Great! But It would be nice If someone could explain why It works.Almond
Where is that file?? I deleted the <projectname>.idl and now the project structure is gone!! furthermore the settings cannot be loaded because the iml file does not existHervey
Perfectly works. I tried changing JDK, invalidating caches, Reimporting project, nothing worked. Deleting main.iml works.Radiotelephony
@Hervey don't delete <projectname>.idl, delete main.idl file, it's inside the main folder :). It worked for me too.Aun
Hey, It worked for me as well. A Big thank you.Storz
M
47

This was mentioned in another answer to this same question here, but this alone fixes this for me. I do all my builds in a separate terminal, outside of IntelliJ. So the cache's need to have the proper permissions set for the IntelliJ app to read them.

Run it from the project's root folder.

$ mvn -U idea:idea
Muslim answered 30/11, 2016 at 22:50 Comment(1)
I think this could help... but unfortuntely running it gives a connection time out. could not connect to repo :(Hervey
A
40

For Gradle users:

You may need to synchronize your project with your build.gradle file.

You can right-click on your gradle file under the Project pane to do this, but that didn't seem to do anything for me (I suspect a bug in my version). You'll know if this happens because it wont' kick off any IntelliJ tasks that you will be waiting on. Instead, open up the Gradle Tool pane, then click the synchronize (refresh) button. This worked for me where invalidating the cache and restarting did not.

My own circumstance: I was using a Scala project with Gradle and had to do this.

Antagonism answered 2/7, 2015 at 21:36 Comment(6)
I just want to say as a side note that I needed to do this again after refactoring my build system a bit; somehow, IntelliJ spotted some issues with my build.gradle file that didn't pop up previously. In the end, was able to get it to all work out fairly quicly.Antagonism
Gradle Tool pane is at: View > Tool Windows > GradlePattern
@Antagonism - I just updated IntelliJ to the latest version as of Sunday August 6, 2017. The problem you mention where right-clicking the build.gradle file in the Project files pane and nothing happens still exists. Thankfully, your tip to use the Refresh button in the Gradle Tool window (View -> Tool Windows -> Gradle) worked great.Kafir
On the note of the build.gradle issues: Sometimes IntelliJ finds nonexistant issues if you have no run configurations. I once had some weird mavenCentral() could not be resolved until I added the jUnit configurations back (I had ported an entire project to Gradle by making a new Gradle project, then copying all source files over).Inward
Unfortunately didn't resolve my issue (nor Invalidating Cache). Unclear on the solution but reorganising the gradle build dependencies for my multiple project removed errors and built successfully. For my case it was using the wrong dependency for the main project.Liard
This worked as a charmIntervocalic
F
23

One extra step, when I did File -> Invalidate Caches and restarted the IDE, open a project. It popped up a toastbox on the top-right asking me whether to enable auto-import and that solved the problem.

Flowery answered 29/11, 2012 at 15:49 Comment(0)
D
17

Inconsistent/duplicate module names in project structure was causing this issue for me.

  1. Go to File -> Project Strucutre -> Modules
  2. In Click on modules which have red underline
  3. Got to "Dependencies" tab
  4. Make sure the dependencies which are red actually exist in dependency list. If not, rename them to match existing dependency names

This can happen when IntelliJ doesn't shut down properly and thus cannot properly save renamed module names.

Derail answered 29/12, 2015 at 12:55 Comment(2)
Finally. I had two modules listed for some reason, "main" and my project name. Removed main, and the errors went away.Balky
Finally, so that's why reimporting the project always works! Because it doesn't duplicate modules on import. But somewhere, somehow, IntelliJ eventually duplicates modules especially during big refactors, and this happens.Kharkov
M
14

Another thing to check: Be sure that dependencies are not duplicated. In my case I found that a module exhibiting this behavior was misconfigured as follows: it had a dependency on another module, and it had a dependency on a jar produced by that other module. This meant for every symbol referenced in duplicate, and was ambiguous.

Modulation answered 6/4, 2012 at 16:17 Comment(0)
C
11

Press "shift" two times > "Reimport All Maven projects" always works for me.

enter image description here

Cartouche answered 10/6, 2020 at 15:57 Comment(1)
Now it's called "Reload All Maven Projects".Cartilage
N
10

Has a fixed been published? Appears issue originally affected v11/12 due to a "Compiler overhaul" back in 2013. With discussion on related issues in Jira up to end of 2014. http://youtrack.jetbrains.com/issue/IDEA-98425

Also on Jira IDEA-98425 was marked fixed but unverified (on v12.0.3). None of the following work-around helped resolve this "Unable to Resolve Symbol" issue with Version 13.1.1 on Windows

a. Delete .IdealIC13 folder (Then, File \ Invalidate Caches / Restart)

b. From Maven Projects window,

b.1 mvn -U idea:idea –〉Executing this maven goal suppose to reload the dependencies. This works prev, but since last FRI, executing this maven goal failed as it tried to recompile the project (Of course it fails as "Unable to resolve Symbols", that's what I am trying to fix by running this command in the first place) mvn -version — shows maven version referenced 3.2.5 and that it's working

b.2 Simply right click project, and Reimport

b.3 File \ Invalidate Caches / Restart

c. Tried both Enable & Disable this setting: File -> Settings -> Maven -> Importing -> "Use maven3 to import project"

d. Settings \ Maven \ Multiproject build fail policy = Fail at end (instead of Default)

Nothing works. What's happenning to IntelliJ support on Maven.

https://youtrack.jetbrains.com/issue/IDEA-99302

From JetBeans release history, https://www.jetbrains.com/company/history.jsp

IntelliJ v14 NOV 2014

IntelliJ v13 DEC 2013

I'd assume v12 fixed (although unverified) would be incorporated in subsequent releases. Any one having similar problems with which IntelliJ version? Please share your experience. IntelliJ maven support seems broken.

Neoprene answered 3/5, 2015 at 3:37 Comment(2)
The problem disappeared... after I "Open" project instead of "Import" project... god damn strange.Neoprene
This worked for me! :) Lost a day of working but it worked. I wonder what's the different between open and importLopeared
T
10

Update 2022

IntelliJ 2022.1 has an interactive, step-by-step process File -> Repair IDE. It leads you through 5 steps in sequence to try and resolve this problem for the current project before invalidating the caches for all projects. From the IDEA documentation:

  1. Refresh Project Indexes
  2. Rescan Project Indexes
  3. Reopen Project
  4. Drop Shared Indexes
  5. Reindex Project
  6. Invalidate Caches and Restart

Steps 1- 3 fixed it for my project.

In v2022.3 and the new UI, this setting is now in File -> Cache Recovery -> Repair IDE. You can also select an individual step from there.

Tejeda answered 14/4, 2022 at 18:49 Comment(1)
I did not see the "Repair IDE" option before, but it fixed my issue when I tried it out. I did not need to do any further steps. Many thanks!Eterne
P
9

None of the other answeres worked for me. My imports were not being resolved because IntelliJ pointed to wrong .m2 file.

IntelliJ Version: IntelliJ Idea 2018.1.5

My location for the .m2 directory was pointed to the wrong path. All I did to fix it was re-point IntelliJ to the right .m2 directory and update it.

First, go to: File->Settings->Build, Execution, Deployment->Build Tools->Maven

I had to change the User settings file: and the Local repository: to the correct location of my .m2 directory.

After this go to: File->Settings->Build, Execution, Deployment->Build Tools->Maven->Repositories

and click the Update button.

Persons answered 21/6, 2018 at 15:29 Comment(1)
My local repository points to wrong place. Your answer saved my life!!!!!Gamo
A
8

I'm jealous of all of you who resolved through File / Invalidate caches. I just spent hours trying everything on this question and a few others from around the web.

The magic menu item didn't do it for me so I invalidated the caches myself by nuking %USERPROFILE%\AppData\Local\JetBrains\IntelliJIdea2020.3\caches

Once I did this and restarted IntelliJ (2020.3), the indexes were rebuilt and my errors went away.

Aman answered 12/2, 2021 at 6:19 Comment(3)
thanks for your solutionBiggin
one can also try File -> Invalidate Caches, seems to be the same thing.Biggin
Yeah it didn't work for me after repeated attempts which is why I went looking for the folder it was trying to clearAman
A
6

My Project Structure:

src -> main -> scala -> mypackages

What worked:

Right click on the scala folder, and click "Mark Directory as Sources Root".

Ancel answered 23/11, 2015 at 2:48 Comment(0)
C
6

If your maven project is a multi-module project, check if some of the modules are ignored by intellij.

  1. Click on View -> Tool Windows -> Maven Projects, and check if some modules are ignored(Ignored modules are in grey, e.g. gs-multi-module in the following image).

enter image description here

  1. Right click on the ignored module and choose Unignore Projects.

enter image description here

Then wait for intellij to finish indexing, after which it will work.

Cartilage answered 20/9, 2018 at 8:38 Comment(1)
Finally this worked for me.Tibetan
N
6

I had problem with Maven Importer JDK - somehow it switched itself to JDK 11, but Maven Project reload worked only with JDK 8.

enter image description here

Noted answered 18/3, 2021 at 14:15 Comment(2)
Same here, had to switch back to JDK 1.8.0_252Serrulation
Solved the problem for me too.Coryza
D
5

For Gradle projects:

  1. Exit IntelliJ IDEA
  2. Delete the <problematic-project-root>/.idea directory
  3. Delete the <problematic-project-root>/.gradle directory
  4. Delete all .iml files in <problematic-project-root>
    • windows command prompt: DEL /S /Q *.iml
    • linux: find . | grep -e .iml$ | xargs rm
  5. Re-import the project into IntelliJ IDEA with Gradle
Disaffirm answered 26/3, 2019 at 15:49 Comment(0)
A
4

In IntelliJ IDEA 2020.3, select:

  1. File > Invalidate Caches / Restart... > Invalidate and Restart

  2. When prompted with Download pre-built shared indexes:

    enter image description here

    don't import the shared indexes: close the dialog or choose More actions > Don't show again

Alisiaalison answered 18/12, 2020 at 9:2 Comment(0)
G
3

Yes, sounds like you have to create libraries containing the JARs you need and add them as a dependency in your module.

Garey answered 6/5, 2011 at 1:27 Comment(0)
S
3

I just had this issue and it would just not go away. I eventually wiped out the IntelliJ config directory in ~ and rebuilt my IntelliJ project from scratch. (This only took about 15 minutes in the end, compared to spending an hour trying to work out problems with cached files, etc.)

Note that my guess is that the initial problem was caused by something like javathings.blogspot.com/2009/11/too-many-open-files-in-intellij-idea.html (NB: as of 2018, that link is dead, but archive.org has a copy of the page from around when this answer was first written -ed.) or a disk space/memory issue causing Java to crash. IntelliJ seemed to just get corrupted.

Schwartz answered 9/9, 2011 at 21:5 Comment(0)
T
3

For another alternative.

I got this problem also when I used JDK7_07. I tried all answers here (except deleting IDEA System Directory). But, I still got the problem. So what I did it is:

Install newest JDK (it was JDK7_45), and set the Intellij's JDK to the new one, and it works.

Transvalue answered 26/11, 2013 at 13:4 Comment(1)
Had the problem with Intellij IDEA 2019.3 and the bundled JDK 11.0.3 and solved it by installing manually JDK 11.0.4, see here: #61338700Cavicorn
E
3

Re-importing the project worked for me. Right Click on Project -> Maven ->Reimport

when I did File -> Invalidate Caches and restarted the IDE,open a project. It showed a dialogue on the top-right "Maven changes detected" and gave an option to import and enable auto-import. Even after importing the project here, I had the same issue. The above step solved the problem.

Erwin answered 9/3, 2015 at 21:19 Comment(0)
A
3

Suggested solutions did not work. I had to unignore several projects, by right clicking on the pom => maven => unignore project.

Then after a

mvn clean install -T 6 -DskipTests

in the console, IntelliJ was happy again. No idea how the projects became ignored...

Ardene answered 8/3, 2017 at 7:59 Comment(0)
O
3

Sometimes, I just open project structure, and click project, then choose a SDK version.

Opprobrious answered 18/4, 2017 at 9:22 Comment(0)
B
3

In my case, I am trying to open a spring boot project from IntellijIdea got the same issue like unable to import all spring related files.

Then I did:

File -> Close Project -> Import Project -> Import from external model -> Choose Gradle -> Next -> Select the project from file location -> Finish

Now everything working fine as expected.

I have seen many answers here but I finally found this solution. It may use for someone like me.

Boyla answered 17/5, 2018 at 18:23 Comment(0)
T
3

I tried

File ->  Invalidate and Restart

which did not worked for me.

Solution that really worked :

Project Structure -> Modules -> select target folder -> Right click generated-sources and select sources -> Apply -> ok

snapshot

Tiphany answered 5/1, 2023 at 5:38 Comment(0)
F
2

After invalidating my cache and restarting; and confirming my maven settings were okay, I was still seeing "Cannot resolve symbol" for a module that I definitely had set as a dependency. Turns out I had it set to the wrong scope.

You can check by right clicking on your module and selecting Open Module Settings. Click the dependency tab and verify that the scope for your un-resolvable dependency is set to Compile.

Freer answered 23/4, 2014 at 16:1 Comment(0)
C
2
  1. Open "Settings"
  2. Search for "Maven"
  3. Click on "Ignored Files" under "Maven"
  4. Uncheck the pom.xml files contains the missing dependencies
  5. Click "OK"
  6. Click File -> Invalidate Caches/Restart...
  7. Click "Invalidate and Restart"
Caruso answered 13/9, 2016 at 7:2 Comment(1)
there are no pom.xml in the screen in 4,Hervey
P
2

mvn idea:idea worked for me. Found it here. Spent more than an hour, hope it helps someone

Preclude answered 14/9, 2017 at 4:4 Comment(0)
M
2

I tried invalidating cache, it didnt work for me.

However, I tried removing the jdk from Platform Settings and added it back and it worked.

Here's how to do it.

Project Settings -> SDKs -> Select the SDK -> Remove (-) -> Add it back again (+)
Minsk answered 22/7, 2020 at 7:5 Comment(0)
U
2

In my case, my project was using Lombok and I was missing the Lombok Plugin in IntelliJ. After installing it everything worked fine.

Uncertainty answered 3/9, 2020 at 8:56 Comment(0)
E
2

For a simple solution, I simply just deleted my version of IntelliJ (and all related files) and reinstalled it. After reimporting the project these errors were gone.

Emendation answered 17/5, 2022 at 19:6 Comment(0)
A
1

In my case, only buildDir is removed. In this case, File | Invalidate Caches does not work.

When I do Build | Make Project before File | Invalidate Caches, everything works fine.

Amrita answered 4/7, 2016 at 9:24 Comment(2)
it threw an error: Java error release vestion 5 is not supportedHervey
@Hervey Sounds like you have your project configured to use Java 5. Most libraries nowadays require at least Java 8, and there's not much reason to use anything lower (lots of things happened in the JDK, both in terms of new features and performance), so try again with Java 8 or higher.Inward
Q
1

None of the above solutions solved it for me. I had the same issue where the code compiled fine but IntelliJ showed that it could not find the import. Even though IntelliJ suggested the import in the first place from code completion.

My solution was to move everything to the default package, delete the com.foo.bar package, then create it again and move everything back.

Quartana answered 27/9, 2016 at 6:22 Comment(0)
E
1

Remove the :iml file found in java and test folders inside the project and invalidate and restart .

It will ask can I remove the projects . Put Yes. The error will goes off.

Edirne answered 28/11, 2017 at 11:50 Comment(0)
T
1

"File -> Invalidate Cache and Restart" will resolve all dependencies.

Timbering answered 18/12, 2017 at 5:40 Comment(1)
Yes, this helped me. if POM.xml is red. click this button and it will work most of time.Intertype
D
1

What worked for me was to "Mark as directory source root" the directory in which the red marked class was, after it the red mark disappeared. Seems that for some reason it was unmarked.

Dody answered 5/1, 2018 at 10:14 Comment(0)
E
1

In my case, a colleague added Lombok to the project, and I had to install the Idea Lombok plug-in. In your case, it may be something else that requires a plug-in.

Emotionalize answered 22/10, 2020 at 11:21 Comment(1)
@Deniz Husaj already wrote the same answer one month and half before yours.Alisiaalison
J
1

For Ivy users and and IntelliJIdea2020.xx

If nothing helps for the already answered,
Close IntelliJ and delete completely the folder:
C:\Users\{username}\AppData\Local\JetBrains\IntelliJIdea2020.xx
Restart IntelliJ project and it should resolve the previous not correctly loaded dependencies.

Jem answered 9/1, 2021 at 13:34 Comment(0)
O
1

I faced a similar kind of problem while using lib build with Kotlin. I included the library through gradle. Intellij was able to run my application but not to resolve some classes and its method. Then I realized that the Kotlin plugin version in my Intellij is outdated and is lower than the Kotlin version that build the library. So I updated the Kotlin plugin from File--> Settings-->Languages & Frameworks-->Kotlin and it resolved the problem.

I tried File | Invalidate Caches, deleting the .idea folder, cache folder, reimporting the project. But nothing worked.

So updating the corresponding plugin can be helpful.

Odey answered 7/3, 2022 at 21:20 Comment(0)
W
1

I had a problem where IntelliJ coudn't resolve classes like String or List and even java. The issue was no SDK was used for the project and going into File> Project Structure > Project > SDK and setting the appropriate SDK and clicking on apply fixed it for me

Word answered 17/4, 2022 at 9:30 Comment(0)
I
0

For Intellij Idea users this commands before even importing project might help :

./gradlew build
./gradlew idea
Impatience answered 22/10, 2015 at 15:43 Comment(0)
P
0

if maven project then just go to settings ->build tools - >maven ->importing. check the checkbox "import maven project automatically". will solve the problem.

Philo answered 1/9, 2016 at 12:13 Comment(0)
H
0

Invalidate Caches worked for me but after running the application had the same error.

So I tried (Intellij):

1 - Menu bar - Refactor | Build | Run | Tools - click Build then Rebuild Project

2 - MVN clean

3 - Right click on project > Maven > Generate Sources and Update Folder

Hope this works for you.

Thanks

Hadlock answered 13/9, 2016 at 17:6 Comment(0)
C
0

Or maybe the file you import is too large.this is my case,when I change the Intellij property: iead.max.intellisen.filesize (path is ${idea dir}/bin/idea.properties) to a larger value,like 25000, and restart the IDE,the problem disappeared. Hope this helps.

Cotoneaster answered 6/1, 2017 at 2:5 Comment(0)
L
0

I had a similar issue when I switched to a new computer.

I copied all the IDEA files (even the related %APPDATA%-folders), but then the Maven build was successfully, but IDEA did not find any classes from the dependencies, when building it.

The solution: I started with a new and clean IDEA profile by removing the folders from the %APPDATA%-folders

Liard answered 5/6, 2017 at 0:15 Comment(0)
I
0

For Idea 2017.1 + Gradle, the plugin is somehow buggy. Tried all synchronize, invalidate + restart, nothing worked. According to https://github.com/gradle/gradle/issues/2315, this worked for me: 1. Close Idea 2. Type gradle idea in command line (should generate 3 files: .iml, .ipr, .iws) 3. Run idea and open the created file .ipr, this should import your project from scratch, with hard-wired dependencies in those 3 files

Interactive answered 3/8, 2017 at 14:9 Comment(1)
However, this is rather temporary solution. I just installed 2017.2.1 and now problem disappeared.Rossiter
M
0

If nothing works out, right click on your source directory , mark directory as "Source Directory" and then right click on project directory and maven -> re-import.

this resolved my issue.

Mending answered 20/10, 2017 at 5:47 Comment(0)
S
0

What helped me to resolve this issue:

I have had another branch, have switched to that branch and built the project with mvn clean install. Everything was ok, then switched back to the master and build the project again and the errors disappeared. Invalidate caches and restart didn't help me.

Shipe answered 5/1, 2018 at 10:8 Comment(0)
Y
0

sometimes, when you create package like com.mydomain.something the directory structure does not get created and you are left with a single folder named "com.mydomain.something" in this case you should create directory structure, like

com
|_mydomain
  |_something 
Yippie answered 1/2, 2018 at 17:23 Comment(0)
P
0

Go to File->Project Structure -> SDKs and check if your SDK file path is correct.

Peccary answered 1/2, 2018 at 21:53 Comment(0)
K
0

Had trouble importing standard java libraries such as java.beans.*

Fixed it on my Redhat 7 system by pointing it to the correct JRE path.

File->ProjectStructure->SDKs->1.8 Changed 'JDK home path:' to /usr/lib/jvm/java-1.8.0-openjdk-1.8.0161-2.b14.el7.x86_64 instead of /usr/lib/jvm/java-1.8.0-openjdk-1.8.0161-2.b14.el7_4.x86_64

The former path to jdk(with the _4 difference in path) hardly had anything in it. It was missing many java libraries.

Khachaturian answered 11/4, 2018 at 0:50 Comment(0)
J
0

If all my pom.xml files are configured correctly and I still have issues with Maven in IntelliJ I do the following steps

  1. Read how to use maven in IntelliJ lately
  2. Make sure IntelliJ is configured to use Bundled Maven 3
  3. Find and TERMINATE actual java process that is executing Maven 3 repos indexing for IntelliJ(Termination can be done while IntelliJ is running). In case any problems with indices or dependencies not available(thought repositories were configured in pom.xml).
  4. Forced Update for all repositories in "IntelliJ / Settings / Build Tools / Maven / Repositories / ". Takes most of the time, disk space and bandwidth! Took me 20+ minutes to update index for Maven central repo.
  5. Hit Re-import all Maven project once again from IntelliJ
  6. It is a good practice to do steps 1-4 to leave IntelliJ and its demon java processes running over night, if you have multiple repositories and a complex project, so you have everything in sync for the next day. You can in theory use all popular indexed repos out there and launch index update for all repos at the same time(I suppose IntelliJ creates a queue and runs updates one after another) then it can take hours to complete(you will need to increase heap space for that too).

P.S. I've spent hours to understand(step 3) that IntelliJ's java demon process that handles maven index update was doing something wrong and I simply need to make IntelliJ to start it from scratch. Problem was solved without even restarting IntelliJ itself.

Juratory answered 9/7, 2018 at 16:8 Comment(0)
M
0

For MAVEN,

I tried all of the above methods, but couldn't find the solution,

So below are the steps I tried(A mix of some solutions),

  1. mvn clean

  2. mvn idea:clean

  3. Invalidate cache and restart
  4. Remove all the extra .iml files you see in the project structure.
  5. At the root directory of your project, run mvn idea:idea and mvn clean install, to reimport all the dependencies of your maven project.

(Make sure you don't have extra .iml files other than the project/subprojects .iml files)

Monachism answered 13/8, 2018 at 7:7 Comment(0)
S
0

check  import Maven projects automatically, fixed my issue

check import Maven projects automatically, fixed my issue. I spent two hours figuring out where I am doing wrong. Finally was able to fix it.

Shivaree answered 23/9, 2018 at 16:51 Comment(0)
B
0

The number one suggestion of Invalidate Caches/Restart... did not work for me nor did any of the other solutions. It ended up being that my maven repos were incorrectly set up, I fixed this by manually overriding the settings.xml and repository directory:

File -> Settings... -> Build, Execution, Deployment -> Build Tools -> Maven

Then for User settings file and Local repository, check the Override and point it to the correct settings.xml and repository directory.

Breastbone answered 24/5, 2019 at 0:9 Comment(0)
S
0

This seems to happen to me mostly when the dependency jar is also a Fat Jar. Fortunately I had control over the building of the Fat Jar, after making it a non Fat Jar then things just worked. None of the other fixes on any of the answers or comments here worked for me. Also probably noteworthy is that my code was in Kotlin and the Fat Jar was bundling some Kotlin dependencies as well.

Soteriology answered 24/5, 2019 at 17:9 Comment(0)
W
0

verify successfully. my idea version is 2019.2

Quick fix: 1.use maven install you dependent module,then reimport all module 2.compile current module,if still not found,close your all idea window,restart idea.

Wellfound answered 6/1, 2020 at 2:53 Comment(0)
B
0
  1. Find out the package in your pom file which can not find.
  2. Then delete them from your local repository.

My project faced the same problem because one of the jars in the pom couldn't load properly.

Backler answered 24/4, 2020 at 9:10 Comment(0)
I
0

I tested all the solutions above and finally decided to empty my Maven repository manually (I actually deleted the .m2 folder on linux). It did the trick.

Ifni answered 4/6, 2020 at 14:36 Comment(0)
G
0

Previous answers didn't work for me. Switched from version 2020.1 to 2018.2 and things seem fine.

Gervase answered 18/6, 2020 at 14:46 Comment(0)
T
0

I used to have 2020.1 community. And I fixed the issue installing 2020.3 ultimate.

This seems to happen to me with a Kotlin library.

None of the other fixes on any of the answers or comments here worked for me.

Trichoid answered 25/4, 2022 at 14:14 Comment(0)
C
0

Invalidate caches & Restart in Intellij worked for me but after restart I need to wait for reindexing everything. For my project it takes for more that 10 minutes.

I've discovered that it wasn't rare to have the problem gone after a simple restart without cache invalidation. Not always but it helps quite often.

Cradlesong answered 12/5, 2022 at 10:1 Comment(0)
S
0

Check dependency has <scope>runtime</scope> if exist then you remove it

Swanson answered 11/9, 2023 at 16:36 Comment(0)
A
0

For me works this.

On the Main menu select File -> Project Structure, select Project in Project Settings -> Click Edit Button.

enter image description here

Or go directly selecting SDKs in Platform Settings then select the right SDK or remove the others.

enter image description here

Armbrecht answered 19/4 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.