Intellij IDEA无法在成功导入的Maven依赖项中找到类

时间:2015-05-16 07:13:46

标签: maven intellij-idea

我正在尝试在我的项目中运行此页面https://code.google.com/p/json-simple/wiki/EncodingExamples中的第一个json简单示例。

我在此页面http://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1

的pom.xml中添加了maven依赖项

Intellij IDEA说一切都很好,依赖成功导入。但是,它在编辑器中找不到任何json-simple类,说“无法解析符号'JSONObject'。 enter image description here

有什么不对?我在添加其他依赖项时遇到了类似的问题,但是我可以通过下载必要的jar并将其作为依赖项添加到“解决”它们而不使用maven。

感谢任何帮助

谢谢

3 个答案:

答案 0 :(得分:19)

我在IntelliJ和依赖项方面也存在很多问题。我将分享一些麻烦的拍摄方法:

  1. 确保pom.xml中包含依赖项。
  2. 这是第一件要做的事情,我假设你已经完成了它。

    对于下一步,您可以使用右侧的maven标签:

    enter image description here

    单击它时看起来像这样:

    enter image description here

    或右键点击pom.xml并选择Maven,如下所示:

    enter image description here

    1. 重新导入
    2. 快速简便,有时候这正是必要的。

      enter image description here

      1. 生成来源和更新文件夹
      2. 有时候来源没有正确生成,那么这就是关键。

        enter image description here

        1. 下载资源
        2. 有时IntelliJ不会自动执行此操作,您可以在Settings > Build, Execution, Deployment > Build tools > Maven > Importing中更改并启用Import Maven projects automativally

          下载源代码如下:

          enter image description here

          设置按钮是Maven选项卡示例图像右侧最远的按钮。

          最后但并非最不重要:

          将光标放在导入的红色部分,(import org.json.simple.JSONObject;部分)等待红色指示灯出现,然后选择“添加到类路径”。

          当其他所有内容都应该时,需要使用导入更新类路径。 IntelliJ并不总是自动执行此操作。

答案 1 :(得分:1)

我通过添加

解决了这个问题
  

<scope>compile</scope>

我缺少的依赖项。似乎compile并不总是默认值。

奇怪的是,当IntelliJ抱怨时,Eclipse能够找到定义。

答案 2 :(得分:1)

尝试了许多尝试后,此解决方案对我有用:

File -> Invalidate Caches/Restart