IntelliJ inspection gives “Cannot resolve symbol” but still compiles code



http://stackoverflow.com/questions/913587/access-denied-when-pushing-to-remote-repository-via-ssh


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?

-------------------------------------------------------------------------------------------------------

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.


### 解决Java中ObjectMapper符号无法解析的错误 在遇到`ObjectMapper`符号无法解析的问题时,通常是因为引入了不正确的依赖包或缺少必要的库文件。为了确保能够正常使用Jackson库中的`ObjectMapper`类,建议按照以下方式调整项目配置。 #### 1. 正确导入依赖项 如果使用Maven构建工具,则应在项目的`pom.xml`文件中添加如下依赖: ```xml <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> <!-- 版本号可根据实际需求调整 --> </dependency> ``` 对于Gradle用户,在`build.gradle`文件内加入相应的声明语句: ```groovy implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2' ``` 通过上述操作可以确保应用程序包含了处理JSON数据所需的全部功能模块[^2]。 #### 2. 验证IDE设置 有时即使已经正确添加了所需jar包,仍然可能出现编译器提示找不到该类的情况。此时应检查开发环境(如IntelliJ IDEA 或 Eclipse)是否已成功加载外部库,并尝试清理缓存重新索引工程结构来解决问题。 另外需要注意的是,某些情况下可能会误将其他框架提供的同名接口当作目标类型而造成混淆,因此务必确认所使用的具体实现来自`com.fasterxml.jackson.databind.ObjectMapper`而非其他路径下的相似命名实体。 #### 3. 使用示例代码验证 完成以上步骤之后可以通过编写简单的测试案例来进行验证: ```java import com.fasterxml.jackson.databind.ObjectMapper; public class Test { public static void main(String[] args) throws Exception{ String jsonStr = "{\"name\":\"John\",\"age\":30}"; ObjectMapper mapper = new ObjectMapper(); User user = mapper.readValue(jsonStr, User.class); System.out.println(user.getName()); System.out.println(user.getAge()); } } class User { private String name; private int age; // Getters and Setters... } ``` 这段代码展示了如何利用`ObjectMapper`实例读取并转换给定的JSON字符串为自定义的对象实例。运行此程序可以帮助判断当前环境中是否存在任何潜在问题影响到`ObjectMapper`的功能调用[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值