PiotrK
PiotrK

Reputation: 1552

Intellij IDEA doesn't see classes but building via Gradle works

I use IntelliJ IDEA 2016.2.4 together with Gradle. When creating an empty project and adding a simple class together with a test class, the project builds fine when running build target through Gradle, but IntelliJ doesn't see e.g. @Test annotation for JUnit which is included in dependencies by default.

When hovering over the "missing" class, IntelliJ offers me to add JUnit4 to classpath. After doing this, nothing changes.

I tried invalidating caches and restarting - no success.

Under "External Libraries", there is "Gradle: junit:junit:4.11", and the project builds fine, so I presume it's an issue with syntax highlighting and autocompletion.

I'm not sure if it matters, but in build.gradle the line that adds a dependency on JUnit (testCompile group: 'junit', name: 'junit', version: '4.11') is highlighted with a comment:

'testCompile' cannot be applied to '(['group':java.lang.String, 'name':java.lang.String,...])' This inspection reports assignments with incompatible types

It works fine for other dependencies I tried, i.e. there's no warning like this. But IntelliJ still doesn't see their classes.

These are my first steps with Gradle, so maybe something is still not configured. Does anyone have an idea what might be wrong?

Upvotes: 8

Views: 8843

Answers (4)

Frankie Drake
Frankie Drake

Reputation: 1388

In case IDEA not recognizes a single class or a couple of classes, what I do is create new class with a different name, past a content of a missing class there, remove the old class and rename new class to a required one. This solves the issue.

Example: IDEA "lost" class SomeEntity.java:

  1. Create class MyAwesomeEntity.java
  2. Past the content of SomeEntity.java to MyAwesomeEntity.java
  3. Remove SomeEntity.java
  4. Rename MyAwesomeEntity.java to SomeEntity.java

voila

Upvotes: 0

Olexander Yushko
Olexander Yushko

Reputation: 2864

I had the same problem after update IDEA to version 2020.3.
IDEA saw JDK libraries and didn't see external libraries.
solution:

File -> Invalidate Caches / Restart... -> Invalidate and Restart

Your project settings will be saved and IDEA continue see all external libraries.

Upvotes: 11

eatSleepCode
eatSleepCode

Reputation: 4637

The cleaner way would be to Invalidate Caches / Restart. You can find this option in File menu.

edit

Intellij Idea uses iml files to resolves class path. There are good chances its not correct.

Upvotes: 5

PiotrK
PiotrK

Reputation: 1552

After trying with Maven and getting the same result, it was clear that it wasn't a problem with Gradle nor JUnit.

Removing the folder ~/.IdeaIC2016.2 did the job.

Upvotes: 2

Related Questions