首页 文章

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

提问于
浏览
10

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

我在这个页面的pom.xml中添加了一个maven依赖项http://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1

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

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

感谢任何帮助

谢谢

2 回答

  • 1

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

    • 确保 pom.xml 中包含依赖项 .

    这是第一件要做的事情,我假设你已经做到了 .

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

    enter image description here

    单击它时看起来像这样:

    enter image description here

    或者右击你的 pom.xml 并选择 Maven 如下:

    enter image description here

    • 重新进口

    它快速而简单,有时候这正是必要的 .

    enter image description here

    • 生成源和更新文件夹

    有时候来源没有正确生成,那么这就是关键 .

    enter image description here

    • 下载源代码

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

    下载源代码如下:

    enter image description here

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

    Last but not least:

    将光标放在导入的红色部分,( import org.json.simple.JSONObject; 部分)等待红色灯出现,然后选择"add to class path" .

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

  • 17

    我通过添加来解决这个问题

    <scope>编译</ scope>

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

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

相关问题