Intellij Cannot resolve symbol on import

前端 未结 29 1905
耶瑟儿~
耶瑟儿~ 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: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.

提交回复
热议问题