[java] Intellij Cannot resolve symbol on import

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:

enter image description here

enter image description here

Anyone have a clue?

This question is related to java intellij-idea import classpath

The answer is


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


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


you can try invalidating the cache and restarting intellij, in many cases it will help.

File -> Invalidate Caches/Restart


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


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.


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


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


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


@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


Missing io? Try import org.openide.util.io.ImageUtilities.


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.


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

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.


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.


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


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


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.


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.


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.


Intelli iDEA causes these stupid troubles @ times. Simple goto pom.xml , right click and do -> Maven -> Reimport.

This should solve the problem.


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.


The same problem. If these methods not work. you can try to delete the lib from local maven repository, and reimport to pom .

mvn repository

Finally it' fine for me.

project


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.


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.


File -> Invalidate Caches/Restart And Build your project


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

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.


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.


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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to intellij-idea

IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Error: Java: invalid target release: 11 - IntelliJ IDEA IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 ERROR Source option 1.5 is no longer supported. Use 1.6 or later Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How to configure "Shorten command line" method for whole project in IntelliJ intellij idea - Error: java: invalid source release 1.9 Failed to resolve: com.google.android.gms:play-services in IntelliJ Idea with gradle

Examples related to import

Import functions from another js file. Javascript The difference between "require(x)" and "import x" pytest cannot import module while python can How to import an Excel file into SQL Server? When should I use curly braces for ES6 import? How to import a JSON file in ECMAScript 6? Python: Importing urllib.quote importing external ".txt" file in python beyond top level package error in relative import Reading tab-delimited file with Pandas - works on Windows, but not on Mac

Examples related to classpath

spark submit add multiple jars in classpath How to resolve this JNI error when trying to run LWJGL "Hello World"? Intellij Cannot resolve symbol on import Eclipse error "Could not find or load main class" "Could not find or load main class" Error while running java program using cmd prompt Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger getting JRE system library unbound error in build path Run a JAR file from the command line and specify classpath How do I resolve ClassNotFoundException? File inside jar is not visible for spring