Java Intellij 无法在导入时解析符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26952078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:33:56  来源:igfitidea点击:

Intellij Cannot resolve symbol on import

javaintellij-ideaimportclasspath

提问by Asaf Ebgi

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.

如果在 import 语句中,我会right-click -> Goto -> the package's declaration看到侧窗格中显示的所有反编译类 -- 包括我需要的类 -- 如果我尝试自动完成 import 语句,我会注意到我需要的类没有出现在落下。

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.

我已经尝试使缓存失效,不起作用。我找不到任何类冲突——我的类路径中没有其他具有相同包名的 jar 文件。我能够将这个类导入到其他项目中。

Please see screen shots:

请看屏幕截图:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Anyone have a clue?

有人有线索吗?

回答by sahitya

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

您可以尝试使缓存无效并重新启动 intellij,在许多情况下它会有所帮助。

File -> Invalidate Caches/Restart

文件 -> 使缓存无效/重新启动

回答by minx

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 scopeto "Compile" rather than "Test" and it works fine for me now.

我遇到了类似的问题,迈克的评论帮助我朝着解决它的方向前进。
尽管所需的库也是我项目中模块的一部分,但它需要更改范围。在模块依赖项中,我将范围更改为“编译”而不是“测试”,现在对我来说效果很好。

回答by Artem Moiseyenko

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).
  1. 项目结构 ( Ctrl+ Alt+ Shift+ S)。
  2. 模块
  3. 选择您的问题模块。
  4. 更改窗口“依赖项”顶部的选项卡。
  5. 检查所需的库(也许,您需要在选项卡“库”中添加指定的库)或模块已在此处列出并且具有正确的范围(主要是“编译”)。

回答by Vipin Thomas

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.

  1. 项目结构 ( Ctrl+ Alt+ Shift+ S)。

  2. 模块

  3. 选择您的问题模块。

  4. 更改窗口“来源”顶部的选项卡。

  5. 删除不需要的源根。保留一个并在此根中添加 src 和测试源根。

回答by sheldonkreger

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.

我尝试使缓存无效并重新启动,但唯一对我有用的是完全清除 .idea 目录,然后从头开始创建一个新项目。

回答by Ming Gong

I also got this error for multiple times when I try to build a new java project.

当我尝试构建一个新的 Java 项目时,我也多次收到此错误。

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 ideato build the gradle stuff for the project.
  4. Add some dependencies in build.gradlefile, and gradle build, reimport the project.
  5. Create User.javaand School.javain 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.
  1. 创建一个空项目,并创建新目录src/main/java
  2. 创建源包net.gongmingqm10.sketch
  3. 使用gradle wrapper,gradle idea为项目构建 gradle 内容。
  4. build.gradle文件中添加一些依赖项,然后gradle build重新导入项目。
  5. 在现有包中创建User.javaSchool.javanet.gongmingqm10.sketch
  6. 当我尝试在 User.java 中使用 import School 时出现错误。
  7. Intellij 一直抱怨无法解析导入时的符号。

Solution:

解决方案:

Build the project first, and mark the main/java as the source root. Create a new directorywith the same name net.gongmingqm10.sketch. Move all the files from the old troubling packages to new package.

首先构建项目,并将main/java标记为源根。创建一个同名的新目录net.gongmingqm10.sketch。将所有文件从旧的麻烦包移动到新包。

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

正如你从树上看到的那样。我有一个名为net.gongmingqm10.sketch. 但是我们真正需要的是三层目录: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.

但是在我在 Intellij 中完成我的项目构建之前,我创建net.gongmingqm19.sketch了一个名为 的新目录,它会给我一个名为的真实目录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.

当我们尝试导入它时。很明显,intellij 是如何导入 weired 目录下名为a.b.c.

回答by neoguy

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.

PS:清除系统缓存将导致清除本地历史记录。

回答by Wenbin

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

我导入的 Maven 项目也有类似的问题。在一个模块中,它无法解析另一模块的一部分的导入符号(是的,可以解析该模块的一部分)。

I changed "Maven home directory" to a newer version solved my issue.

我将“Maven 主目录”更改为较新版本解决了我的问题。

Update: Good for 1 hour, back to broken status...

更新:好 1 小时,回到破碎状态......

回答by Shanjun Li

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

失踪io?试试import org.openide.util.io.ImageUtilities

回答by crazycode

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.

在我的例子中,我添加了一个包含一些 java 源文件的 jar 文件,但我认为 java 源是坏的,在 Intellij Idea 依赖库中它自动添加源,所以在编辑器中导入是错误的,只需删除“中的源代码”项目结构”->“库”,它对我有用。