IntelliJ 检查给出“无法解析符号"但仍然编译代码 [英] IntelliJ inspection gives "Cannot resolve symbol" but still compiles code

查看:49
本文介绍了IntelliJ 检查给出“无法解析符号"但仍然编译代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

平台:IntelliJ 社区版 10.0.3
SDK:jdk1.6.0_21
操作系统:Windows 7

Platform: IntelliJ Community Edition 10.0.3
SDK: jdk1.6.0_21
OS: Windows 7

所以我在 IntelliJ 上遇到了一个奇怪的情况,这让我完全被难住了.我设置了一个 Maven 项目并在 pom.xml 文件中添加 log4j 作为依赖项.IDEA 检查运行良好,我的单元测试全部编译并运行.

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.

然后我使用 mvn install:install-file 将 hunnysoft 的 jmime 库添加到我的本地 maven 存储库,如下所示.

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 将 jar 文件安装到我的本地存储库中.

Maven installed the jar file just fine into my local repository.

然后我进入 IntelliJ 的设置 => Maven => 存储库服务并更新了我的本地存储库(以便 IntelliJ 重新索引存储库内容).

I then went into IntelliJ's Settings => Maven => Repository Services and Updated my local repository (so that IntelliJ would reindex the repository contents).

最后,我在我的 pom.xml 文件中添加了以下依赖项(就在 log4j 依赖项之上).

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;
    }
}

现在是奇怪的地方.IntelliJ 的意图机制拾取并识别 maven pom 文件中的 Logger 导入就好了.但是,对于所有 hunnysoft 导入,它报告:无法解析符号 'ByteString/Field/FieldBody'",BUT Build => Compile 'StackOverflowQuestion.java' 正确编译所有内容和我创建的单元测试对于这个类运行良好(尽管意图也将 create() 的调用标记为问题区域).

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

所以在某处,IntelliJ 以某种方式忽略了意图子系统的 jmime.jar 文件.我很困惑,因为 log4j 依赖项工作正常,一切都编译和运行正常.F12(转到声明")适用于 Logger 导入,但会中断所有 jmime 导入.

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.

哦,还有一件事,如果我转到项目"窗口中的包"视图,则会出现com.hunnysoft.jmime"包,我可以看到我在上面的代码片段中导入的所有类图书馆".从 pom.xml 文件中删除上述依赖会导致该包消失,编译中断.

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.

似乎检查的类路径已损坏,但在 Settings => Intentions | 中的任何地方似乎都没有对此进行设置.编译器区域(不是我期望的任何此类设置,我相信 IDEA 应该已经知道基于 pom 文件和 JDK 的正确类路径).

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

作为最后的实验,我创建了一个全新的标准 J2SE 应用程序项目(不使用 maven)并将 jmime.jar 文件作为其库之一直接添加到项目中.我在这个新项目中遇到了与上述完全相同的问题.

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.

这是来自 jmime jar 文件的 MANIFEST.MF.

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.

我在这个 jar 文件中没有看到任何异常.

I don't see anything unusual in this jar file.

我最好的猜测是,问题可能是缺少依赖项问题.但是 AFAIK jmime 应该是自包含的(JarAnalyzer 没有提供任何东西,但我不确定如果缺少依赖 jar 会不会).

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?

推荐答案

这在同一问题的另一个回答中提到了此处,但仅此一项就为我解决了这个问题.我在 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.

$ mvn -U idea:idea

这篇关于IntelliJ 检查给出“无法解析符号"但仍然编译代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆