Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij IDEA can't find a class in successfully imported Maven dependency

I'm trying to run the first json-simple example from this page https://code.google.com/p/json-simple/wiki/EncodingExamples in my project.

I've added a maven dependency in my pom.xml from this page http://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1

Intellij IDEA says that everything is fine, dependency is successfully imported. However, it cannot find any json-simple classes in the editor, saying "Cannot resolve symbol 'JSONObject'. enter image description here

What can be wrong? I had similar issues with adding other dependencies, but i could "solve" them by downloading the necessary jar and adding it as a dependency without using maven.

Appreciate any help

Thank you

like image 649
Vadim Avatar asked May 16 '15 07:05

Vadim


People also ask

Why is IntelliJ not importing Maven dependencies?

If the dependencies weren't imported correctly (IntelliJ IDEA highlights them), try to perform the following actions: You can check your local maven repository in the Maven | Repositories settings and try to update it. You can check the jar file of the local . m2 repository to see if it was downloaded correctly.

How clean install Maven dependencies IntelliJ?

Click Run -> Edit Configurations -> Press + -> Search for "Maven" -> Locate "Command Line" field and enter in following maven commands "clean install" -> Press OK. Now click the green button to run that Run/Debug Configuration, this will maven clean install the module/project.


3 Answers

I solve this problem by adding

<scope>compile</scope>

to my missing dependency. It seems that compile is not always the default.

Strangely while IntelliJ was complaining, Eclipse was able to find the definition.

like image 124
Pierre Thibault Avatar answered Oct 18 '22 03:10

Pierre Thibault


I'm also having a lot of issues with IntelliJ and dependencies. I'm gonna share some of my trouble shooting methods for it:

  1. Make sure that the pom.xml has the dependencies in it.

This is the very first thing to do, and I assume you've already done it.

For the next step you can either use the maven tab on the right:

enter image description here

which looks like this when you click it:

enter image description here

or right click your pom.xml and pick Maven like so:

enter image description here

  1. Reimport

It's quick and easy and sometimes that's exactly what's neccessary.

enter image description here

  1. Generate sources and Update Folders

Sometimes the sources haven't been properly generated, then this is the key.

enter image description here

  1. Download Sources

Sometimes IntelliJ doesn't do this automatically, something you can change in Settings > Build, Execution, Deployment > Build tools > Maven > Importing and enable Import Maven projects automativally.

The download Sources looks like this:

enter image description here

The settings button is the one furthest on the right on my example images of the Maven tab.

Last but not least:

Put your cursor on the red part of the import, (the import org.json.simple.JSONObject; part) wait for the red lamp to appear, and the choose "add to class path".

When all else is as it should, the class path needs to be updated with your import. IntelliJ doesn't always do this automatically either.

like image 37
Gemtastic Avatar answered Oct 18 '22 01:10

Gemtastic


this solution worked for me after trying numerous things:

File -> Invalidate Caches/Restart
like image 1
windex Avatar answered Oct 18 '22 01:10

windex