ChatGPT解决这个技术问题 Extra ChatGPT

IntelliJ inspection gives "Cannot resolve symbol" but still compiles code

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?

See also stackoverflow.com/questions/15046764/… if it cannot resolve symbols that are part of maven dependencies [for me it was a missing M2_HOME]
2018.3.3. Still the same error...
File>Invalidate Caches/Restart worked for me in version 2020.03.
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 install

P
Pablo

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.


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.
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.
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 support@jetbrains.com.
The menu option is now "File | Invalidate Caches/Restart" (Android Studio 0.8.2, linux).
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.
r
rshahriar

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


How do i do this in IntelliJ for Mac OS ?
@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"
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.
for 2017.1.x is working reimport maven projects: View -> Tool Windows -> Maven Projects, then click on cycle arrows icon.
Reimporting the Maven module helped me too, I was surprised it isn't reimported as dependencies change anyway...
c
cerisier

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


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.
Worked! Great! But It would be nice If someone could explain why It works.
Where is that file?? I deleted the .idl and now the project structure is gone!! furthermore the settings cannot be loaded because the iml file does not exist
Perfectly works. I tried changing JDK, invalidating caches, Reimporting project, nothing worked. Deleting main.iml works.
@KansaiRobot don't delete .idl, delete main.idl file, it's inside the main folder :). It worked for me too.
T
Tom

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

I think this could help... but unfortuntely running it gives a connection time out. could not connect to repo :(
b
bbarker

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.


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.
Gradle Tool pane is at: View > Tool Windows > Gradle
@bbarker - 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.
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).
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.
M
Matthew Chen

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.


B
Babken Vardanyan

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

Go to File -> Project Strucutre -> Modules In Click on modules which have red underline Got to "Dependencies" tab 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.


Finally. I had two modules listed for some reason, "main" and my project name. Removed main, and the errors went away.
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.
L
Landon Kuhn

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.


佚名

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.


The problem disappeared... after I "Open" project instead of "Import" project... god damn strange.
This worked for me! :) Lost a day of working but it worked. I wonder what's the different between open and import
C
Chirag Visavadiya

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

https://i.stack.imgur.com/DI0ki.png


Now it's called "Reload All Maven Projects".
T
Tim

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.


My local repository points to wrong place. Your answer saved my life!!!!!
a
axiom

My Project Structure:

src -> main -> scala -> mypackages

What worked:

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


J
Josh

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.


thanks for your solution
one can also try File -> Invalidate Caches, seems to be the same thing.
Yeah it didn't work for me after repeated attempts which is why I went looking for the folder it was trying to clear
S
Searene

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

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).

https://i.stack.imgur.com/bgaro.png

Right click on the ignored module and choose Unignore Projects.

https://i.stack.imgur.com/HhBEC.png

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


H
Hourglasser

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

https://i.stack.imgur.com/m4AI6.png


Same here, had to switch back to JDK 1.8.0_252
Solved the problem for me too.
E
Eric

For Gradle projects:

Exit IntelliJ IDEA Delete the /.idea directory Delete the /.gradle directory Delete all .iml files in windows command prompt: DEL /S /Q *.iml linux: find . | grep -e .iml$ | xargs rm Re-import the project into IntelliJ IDEA with Gradle


d
duffymo

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


i
ismailsunni

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.


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: stackoverflow.com/questions/61338200/…
S
Sandeep

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.


R
Ronald Teune

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...


H
Hashbrown

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


S
Shailendra Madda

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.


S
SOLO

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.


u
user3565334

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.


J
Jasper Citi

Open "Settings" Search for "Maven" Click on "Ignored Files" under "Maven" Uncheck the pom.xml files contains the missing dependencies Click "OK" Click File -> Invalidate Caches/Restart... Click "Invalidate and Restart"


there are no pom.xml in the screen in 4,
D
Deniz Husaj

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


M
Marco Lackovic

In IntelliJ IDEA 2020.3, select:

File > Invalidate Caches / Restart... > Invalidate and Restart When prompted with Download pre-built shared indexes: don't import the shared indexes: close the dialog or choose More actions > Don't show again


c
chiguri

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.


it threw an error: Java error release vestion 5 is not supported
@KansaiRobot 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.
T
Trinimon

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.


n
nilesh

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