15

I am having trouble compiling a Maven project in IntelliJ.

  • If I run mvn clean install from commandline = SUCCESS
  • If I click on the "Reimport All Maven Projects" icon in IntellJ = SUCCESS
  • If I click on the "Generate Sources and Update Folders For All Projects" icon in IntelliJ = SUCCESS
  • If I click on the "install" Lifecyclein the Maven Projects panel = SUCCESS
  • After the Maven "install" I can even run the application without problems and I can see my latest source code changes.

Therefore I don't believe there is anything wrong with the project itself. But

  • IntelliJ shows there are a lots of methods and fields missing (red highlights)
  • Make Module ... fails
  • Compile Module ... fails
  • All Unit Tests running from IntelliJ fail with "java: cannot find symbol" errors, but PASS from the command line

I have

  • restarted the IDE several times
  • Click File -> Invalidate Caches/Restart...
  • Click File -> Project Structure... -> Problems = Empty panel (I assume it means no problems)
  • Click File -> Settings... -> Maven = Maven settings is correct

What else could I try to make IntelliJ intelligent enough to compile the project?

Jasper Citi
  • 1,673
  • 1
  • 23
  • 31
  • Have you tried to delete the module and re-import it in IntelliJ? This helped me several times. – Dominik Sandjaja Jun 28 '17 at 08:09
  • you might try to close the project in IntelliJ, then delete the _.idea_ folder and the _*.iml_ file in your project, after that reopen the project in IJ – P.J.Meisch Jun 28 '17 at 08:46
  • I was initially a bit sceptic about removing the module, but after I had removed it (also physically deleting all files including the iml files) and re-imported everything, all hell broke loose with errors everywhere. After I sorted out all these errors, IntelliJ compiled my project successfully and even the Unit Tests now work. So who gets the points for the answer as I followed both Dominik and P.J.Meisch's advice? – Jasper Citi Jun 28 '17 at 09:16
  • See http://stackoverflow.com/a/42427510/104891. – CrazyCoder Jun 28 '17 at 12:31
  • Probably a caching problem, try `File | Invalidate Caches/Restart | Invalidate` and in your `Maven Projects` tool click `Lifecycle | clean` and/or the `Generate Sources and Update Folders For All Projects` button – krevelen Apr 19 '18 at 12:16
  • Another thing that I discovered that could also cause this issue is, my colleague initially Capitalized all his Java package names. Our tech lead requested that he renamed it to lower case to conform with the standard which he did, but the folders remained in Uppercase. Maven happily compiled on Windows without any issues, but IntelliJ did not like it at all. – Jasper Citi Jan 22 '20 at 09:27
  • **Invalidate Caches/Restart...** actually fixed the problem for me, thanks! – notacorn Apr 26 '21 at 18:02

8 Answers8

11

This can happen if command line uses a different version of mvn than IntelliJ. Make sure that Intellij is using the same maven executable that command line is using. First type: "which mvn" to find out the mvn executable that command line is using. Then make IntelliJ use the same version by going to: Intellij => Preferences => Build, Execution and Deployment => Build Tools => Maven => Maven Home Directory. Mvn Settings in IntelliJ 2017.1

ssiddiqu
  • 111
  • 1
  • 3
6

Just ran into this problem. What worked for me was to close the project and delete the .idea folder.

Reopened the project then set it up as a maven project again...

All good after that.

awhig
  • 507
  • 1
  • 7
  • 13
2

Check the JDK setting for the project. In my experience, that is the most common cause of bizzare Intellij-cannot-compile problems.

Under "Project Settings" (CMD-,) - see the "Project SDK" setting.

Make sure it's a valid SDK, and the version matches the required version for the sources (i.e. check the maven compiler plugin source/target settings, and make sure the selected SDK is at least that version, or higher).

ash
  • 4,867
  • 1
  • 23
  • 33
1

After a few years, I came across the same issue again but with another project. In this case IntelliJ was missing the Lombok plugin which generates the missing sources.

To install:

  1. Go to: File -> Settings -> Plugins
  2. Type "Lombok" in the search bar
  3. Click "Install" next to Lombok
  4. Restart IntelliJ
Jasper Citi
  • 1,673
  • 1
  • 23
  • 31
1

I faced similar problem. Solution is, set the IntelliJ maven home directory to the installed maven location. Goto File->Settings And then set the maven home directory as shown in the image:

enter image description here

Junaed
  • 1,457
  • 13
  • 15
0

I've had this issue when I've had generated sources. Maven wasn't setup to mark the compiled-test-sources folder as a test sources root, so every time I did something with Maven I had to mark the generated folders.

Ret
  • 1
  • 1
0

I encountered a similar issues with compiling to Java 11 from IntelliJ Maven compile indicating that com.sun.management.HotSpotDiagnosticMXBean#dumpHeap() did not exist.

After trying the above steps, I still could not get it to resolve. I had recently migrated to a new instance of Ubuntu and decided to fire up my old VM, where the code compiled without an issue. So it occurred to me that when I installed IntelliJ on the new VM, I chose to download AdoptOpenJDK 11 (adopt-openj9-11.0.7) via IntelliJ SDK download feature.

So I manually downloaded and installed a fresh copy of OpenJDK 11 (jdk-11.0.6+10), then changed the project's default SDK to the new one I installed, and everything compiled just fine.

I hope this answer may help others.

tparton42
  • 31
  • 3
0

Have been facing this issue for long now, and usually the root cause for it is different maven version running on IntelliJ as mentioned above, but also sometimes it can be caused by different version of Java selected in the project structure.

As for me even after aligning the maven version the issue would not go, but once I checked the java version, Intellij had by defaulted selected Java 11 for me, and once I changed it back to Java 8, it started working again.

Mukul Sharma
  • 1
  • 1
  • 2