Intellij Cannot resolve symbol on import

前端 未结 29 1739
耶瑟儿~
耶瑟儿~ 2020-11-30 18:10

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\'

相关标签:
29条回答
  • 2020-11-30 19:05

    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.

    0 讨论(0)
  • 2020-11-30 19:05

    @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

    0 讨论(0)
  • 2020-11-30 19:07

    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
    0 讨论(0)
  • 2020-11-30 19:07

    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).
    0 讨论(0)
  • 2020-11-30 19:07

    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.

    0 讨论(0)
  • 2020-11-30 19:08

    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.

    0 讨论(0)
提交回复
热议问题