IntelliJ 检查给出“无法解析符号”但仍然编译代码
平台:IntelliJ 社区版 10.0.3
SDK:jdk1.6.0_21
操作系统:Windows 7
所以我在使用 IntelliJ 时遇到了一个奇怪的情况,这让我完全陷入困境。我设置了一个 Maven 项目,并将 log4j 添加为 pom.xml 文件中的依赖项。 IDEA 检查运行良好,我的单元测试全部编译并运行。
然后,我使用 mvn install:install-file 将 hunnysoft 的 jmime 库添加到我的本地 Maven 存储库,如下所示。
mvn install:install-file -Dfile=jmime.jar -DgroupId=jmime \
-DartifactId=jmime -Dversion=3.1.1e -Dpackaging=jar
Maven 将 jar 文件很好地安装到了我的本地存储库中。
然后我进入 IntelliJ 的设置 => Maven=>存储库服务并更新了我的本地存储库(以便 IntelliJ 重新索引存储库内容)。
最后,我将以下依赖项添加到我的 pom.xml 文件中(位于 log4j 依赖项之上)。
<dependency>
<groupId>jmime</groupId>
<artifactId>jmime</artifactId>
<version>3.1.1e</version>
</dependency>
我现在创建一个新类,如下所示:
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;
}
}
现在来看看奇怪的地方。 IntelliJ 的意图机制可以很好地识别并识别 maven pom 文件中的 Logger 导入。但是,对于所有 hunnysoft 导入,它都会报告:“无法解析符号 'ByteString/Field/FieldBody'”,BUT Build =>编译“StackOverflowQuestion.java”可以正确编译所有内容,并且我为此类创建的单元测试运行良好(尽管其意图也将对 create() 的调用标记为问题区域)。
因此,在某个地方,IntelliJ 会忽略意图子系统的 jmime.jar 文件。我很困惑,因为 log4j 依赖项工作正常,并且一切都编译并运行良好。 F12(“转到声明”)适用于 Logger 导入,但会中断所有 jmime 导入。
哦,另一件事,如果我进入“项目”窗口中的“包”视图,则会出现“com.hunnysoft.jmime”包,我可以看到我在上面的代码片段中“库”下导入的所有类。从 pom.xml 文件中删除上述依赖项会导致该包消失并且编译中断。
看来检查的类路径已损坏,但“设置=>”中的任何位置似乎都没有对此进行设置。意向 |编译器区域(不是我期望的任何此类设置,我相信 IDEA 应该已经知道基于 pom 文件和 JDK 的正确类路径)。
作为最后的实验,我创建了一个全新的标准 J2SE 应用程序项目(不使用 maven),并将 jmime.jar 文件作为其库之一直接添加到该项目中。我在这个新项目中遇到了与上述完全相同的问题。
这是 jmime jar 文件中的 MANIFEST.MF。
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.
我在这个 jar 文件中没有看到任何异常。
我最好的猜测是,问题可能是缺少依赖项问题。但 AFAIK jmime 应该是自包含的(JarAnalyzer 没有提供任何内容,但我不确定如果缺少依赖项 jar 是否会提供)。
那么,有人有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
首先你应该尝试
File |使缓存无效
,如果没有帮助,请删除 IDEA 系统目录。然后重新导入Maven项目看看是否有帮助。在某些奇怪的情况下,编译的类可能会报告错误的信息并混淆 IDEA。使用 javap< 验证此 jar 中的类报告的名称是否正确/a>.
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.
以下技巧为我解决了这个问题:
我的想法版本是 12.0.4
The following trick resolved this issue for me:
My idea version is 12.0.4
上述解决方案都不适合我。所做的就是手动删除 main.iml 文件,它突然起作用了。
None of the solutions above worked for me. What did was removing the main.iml file manually and it suddenly worked.
在同一问题的另一个答案此处中提到了这一点,但这本身就解决了这个问题。我在 IntelliJ 之外的单独终端中进行所有构建。因此,缓存需要为 IntelliJ 应用程序设置适当的权限才能读取它们。
从项目的根文件夹运行它。
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.
对于 Gradle 用户:
您可能需要将项目与
build.gradle
文件同步。您可以右键单击“项目”窗格下的 gradle 文件来执行此操作,但是这似乎对我没有任何作用(我怀疑我的版本中存在错误)。您会知道是否发生这种情况,因为它不会启动您将等待的任何 IntelliJ 任务。相反,打开 Gradle 工具窗格,然后单击同步(刷新)按钮。这对我有用,但使缓存无效并重新启动却不起作用。
我自己的情况:我正在使用带有 Gradle 的 Scala 项目,并且必须这样做。
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.
当我执行 File -> 时,还有一个额外的步骤:使缓存无效并重新启动 IDE,打开项目。它在右上角弹出一个吐司框,询问我是否启用自动导入,这解决了问题。
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.
项目结构中不一致/重复的模块名称导致了我这个问题。
文件 ->项目结构->模块
当 IntelliJ 未正确关闭并因此无法正确保存重命名的模块名称时,可能会发生这种情况。
Inconsistent/duplicate module names in project structure was causing this issue for me.
File -> Project Strucutre -> Modules
This can happen when IntelliJ doesn't shut down properly and thus cannot properly save renamed module names.
另一件需要检查的事情是:确保依赖关系不重复。在我的例子中,我发现表现出这种行为的模块配置错误,如下所示:它依赖于另一个模块,并且依赖于另一个模块生成的 jar。这意味着每个符号都被重复引用,并且是不明确的。
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.
按“shift”两次> “重新导入所有 Maven 项目”总是对我有用。
Press "shift" two times > "Reimport All Maven projects" always works for me.
已经发布修复了吗?由于 2013 年的“编译器大修”,出现的问题最初影响 v11/12。截至 2014 年底,Jira 中相关问题的讨论。
http://youtrack.jetbrains.com/issue/IDEA-98425
也在 Jira IDEA-98425 上 被标记为已修复,但未经验证(在 v12.0.3 上)。以下解决方法均无法帮助解决 Windows 上版本13.1.1的“无法解析符号”问题
。删除.IdealIC13文件夹(然后,文件\无效缓存/重新启动)
b。从 Maven Projects 窗口,
b.1 mvn -U idea:idea –〉执行此 Maven 目标假设重新加载依赖项。这在之前是有效的,但是自从上周五以来,执行这个 Maven 目标失败了,因为它尝试重新编译项目(当然它失败为“无法解析符号”,这就是我首先尝试通过运行此命令来修复的问题) mvn -version — 显示引用 3.2.5 的 Maven 版本,并且它正在运行
b.2 只需右键单击项目,然后重新导入
b.3 文件 \ 无效缓存/重新启动
c.尝试了启用和启用禁用此设置:文件 ->设置-> Maven->导入-> “使用maven3导入项目”
d.设置 \ Maven \ 多项目构建失败策略 = 结束时失败(而不是默认)
没有任何效果。 Maven 上的 IntelliJ 支持发生了什么变化。
https://youtrack.jetbrains.com/issue/IDEA-99302
根据 JetBeans 发布历史记录,https://www.jetbrains.com/company/history.jsp
IntelliJ v14 一月2014
IntelliJ v13 2013 年 12 月
我假设 v12 已修复(尽管未经验证)将合并到后续版本中。有人在使用哪个 IntelliJ 版本时遇到类似问题吗?请分享您的经验。 IntelliJ Maven 支持似乎已损坏。
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.
更新 2022
IntelliJ 2022.1 有一个交互式的分步过程
文件 ->修复 IDE
。它会引导您依次完成 5 个步骤,在使所有项目的缓存失效之前尝试解决当前项目的此问题。 来自 IDEA 文档:步骤 1-3 修复了我的项目。
在 v2022.3 和新 UI 中,此设置现在位于
File ->缓存恢复->修复 IDE
。您还可以从那里选择单个步骤。Update 2022
IntelliJ 2022.1 has an interactive, step-by-step process
File -> Repair IDE
. It leads you through 5 steps in sequence to try and resolve this problem for the current project before invalidating the caches for all projects. From the IDEA documentation:Steps 1- 3 fixed it for my project.
In v2022.3 and the new UI, this setting is now in
File -> Cache Recovery -> Repair IDE
. You can also select an individual step from there.其他答案都不适合我。我的导入未得到解决,因为 IntelliJ 指向错误的 .m2 文件。
IntelliJ 版本:IntelliJ Idea 2018.1.5
我的 .m2 目录位置指向了错误的路径。我所做的就是将 IntelliJ 重新指向正确的 .m2 目录并更新它。
首先,转到:文件 -> 设置 -> 构建、执行、部署 -> 构建工具 -> Maven
我必须更改用户设置文件:和本地存储库:到我的 .m2 目录的正确位置。
之后转到:文件->设置->构建、执行、部署->构建工具->Maven->存储库
,然后单击“更新”按钮。
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.
我很嫉妒你们所有通过文件/无效缓存解决问题的人。我只是花了几个小时尝试了这个问题以及网络上其他一些问题的所有内容。
神奇的菜单项并没有为我做到这一点,所以我自己通过核弹
%USERPROFILE%\AppData\Local\JetBrains\IntelliJIdea2020.3\caches
使缓存失效。一旦我这样做并重新启动 IntelliJ (2020.3 ),索引被重建,我的错误消失了。
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.
我的项目结构:
有效方法:
右键单击
scala
文件夹,然后单击“将目录标记为源根目录”。My Project Structure:
What worked:
Right click on the
scala
folder, and click "Mark Directory as Sources Root".如果您的maven项目是多模块项目,请检查某些模块是否被intellij忽略。
gs-multi-module
)。Unignore Projects
。然后等待intellij完成索引,之后就可以工作了。
If your maven project is a multi-module project, check if some of the modules are ignored by intellij.
gs-multi-module
in the following image).Unignore Projects
.Then wait for intellij to finish indexing, after which it will work.
我遇到了 Maven Importer JDK 的问题 - 不知何故它自动切换到 JDK 11,但 Maven 项目重新加载仅适用于 JDK 8。
I had problem with Maven Importer JDK - somehow it switched itself to JDK 11, but Maven Project reload worked only with JDK 8.
对于 Gradle 项目:
/.idea
目录/。 gradle
目录
中的所有.iml
文件DEL /S /Q *.iml
查找 . | grep -e .iml$ | grep -e .iml$ | grep -e .iml$ xargs rm
For Gradle projects:
<problematic-project-root>/.idea
directory<problematic-project-root>/.gradle
directory.iml
files in<problematic-project-root>
DEL /S /Q *.iml
find . | grep -e .iml$ | xargs rm
在 IntelliJ IDEA
2020.3
中,选择:文件 > 使缓存无效/重新启动... > 无效并重新启动
当提示下载预构建的共享索引时:
不导入共享索引:关闭对话框或选择更多操作 > 不再显示
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
是的,听起来您必须创建包含所需 JAR 的库并将它们添加为模块中的依赖项。
Yes, sounds like you have to create libraries containing the JARs you need and add them as a dependency in your module.
另一种选择。
我使用JDK7_07时也遇到这个问题。我在这里尝试了所有答案(删除 IDEA 系统目录除外)。但是,我仍然遇到问题。所以我所做的是:
安装最新的JDK(它是JDK7_45),并将Intellij的JDK设置为新的,并且它可以工作。
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.
重新导入该项目对我有用。
右键单击项目 -> Maven ->
当我执行 File -> 时重新导入 ->使缓存无效并重新启动 IDE,打开项目。它在右上角显示了一个对话框“检测到 Maven 更改”,并提供了导入和启用自动导入的选项。即使在这里导入项目后,我也遇到了同样的问题。通过上面的步骤就解决了这个问题。
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.
建议的解决方案不起作用。我必须通过右键单击 pom => 来忽略几个项目。行家 =>忽略项目。
然后
在控制台中敲了一声之后,IntelliJ 又高兴了。不知道这些项目是如何被忽视的......
Suggested solutions did not work. I had to unignore several projects, by right clicking on the pom => maven => unignore project.
Then after a
in the console, IntelliJ was happy again. No idea how the projects became ignored...
有时,我只是打开项目结构,然后单击项目,然后选择 SDK 版本。
Sometimes, I just open project structure, and click project, then choose a SDK version.
就我而言,我尝试从 IntellijIdea 打开一个 Spring Boot 项目,但遇到了同样的问题,例如无法导入所有 Spring 相关文件。
然后我做了:
现在一切都按预期正常工作。
我在这里看到了很多答案,但我终于找到了这个解决方案。它可能对像我这样的人有用。
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:
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.
我刚刚遇到了这个问题,而且它不会消失。我最终清除了
~
中的 IntelliJ 配置目录,并从头开始重建我的 IntelliJ 项目。 (与花费一个小时尝试解决缓存文件等问题相比,这最终只花了大约 15 分钟)请注意,我的猜测是最初的问题是由
javathings.blogspot.com 之类的东西引起的/2009/11/too-many-open-files-in-intellij-idea.html
(注意:截至 2018 年,该链接已失效,但 archive.org 有 第一次编写此答案时的页面副本 -ed。)或导致 Java 崩溃的磁盘空间/内存问题。 IntelliJ 似乎刚刚被损坏了。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.就我而言,我的项目使用 Lombok 并且我缺少 IntelliJ 中的 Lombok 插件。安装后一切正常。
In my case, my project was using Lombok and I was missing the Lombok Plugin in IntelliJ. After installing it everything worked fine.
使我的缓存无效并重新启动后;并确认我的 Maven 设置没问题,我仍然看到我明确设置为依赖项的模块的“无法解析符号”。原来我把它设置到了错误的范围。
您可以通过右键单击您的模块并选择打开模块设置来进行检查。单击依赖项选项卡并验证无法解析的依赖项的范围是否设置为“编译”。
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.
mvn idea:idea
为我工作。在此处找到了它。花了一个多小时,希望对大家有帮助mvn idea:idea
worked for me. Found it here. Spent more than an hour, hope it helps someone我尝试使缓存无效,但它对我不起作用。
但是,我尝试从平台设置中删除jdk并将其添加回来,它起作用了。
以下是具体操作方法。
I tried invalidating cache, it didnt work for me.
However, I tried removing the jdk from Platform Settings and added it back and it worked.
Here's how to do it.