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

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

问题描述

平台:IntelliJ Community Edition 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项目,并将log4j作为依赖项添加到pom.xml文件中。 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的Settings => Maven => Repository Services并更新了我的本地存储库(以便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', Build =>编译'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.

哦,另外一件事,如果我去'套餐'在Projects窗口中查看com.hunnysoft.jmime包,我可以在Libraries下面的代码片段中看到我导入的所有类。从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).

那么,任何人都有任何IDAs?

So, anyone have any IDEAs?

推荐答案

首先你应该尝试文件|使缓存无效如果它没有帮助,请删除 IDEA系统目录。然后重新导入Maven项目并查看它是否有帮助。

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.

在一些奇怪的情况下,编译的类可能会报告错误信息并混淆IDEA。使用 javap <验证此jar中的类是否报告了正确的名称/ a>。

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.

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

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