IntelliJ IDE | .iml File lost or deleted

Solution 1:

*.iml files have nothing to do with your code/Java. It is a hidden file created by Intellij on the root folder of your project which contains your module information. This is the reason you should not version this kind of file, afterall other developers using a different IDE won't need them.

If you closed Intellij, I think there isn't a way to get the same file back, but on the other hand it is pretty simple to regenerate a new .iml file:

Gradle based project

Click the Sync Project with Gradle Files button (this one enter image description here) in the toolbar;

Maven based project

Go to File > Project Structures > Modules > Add > Import Module > (select your modules base directory) > OK

Edit:

My previous answer were based on IntelliJ 14 and it seems some things have changed on the newer versions, as pointed by hBrent and BeC on the comments.

Using Intellij Community 2018.1 for this update:

Maven based project

The procedure remains the same, the only difference I saw is the Add icon isn't green anymore. One can also try removing the rootFolder/.idea/modules.xml and adding the modules afterwards to reset all information regarding the previous configurations.

Gradle based project

Should use "Refresh all Gradle projects" on the Gradle Tool Window (View > Tool Windows > Gradle), represented by the standard refresh icon.

Solution 2:

My mvn project was not also loading correctly in IntelliJ IDEA after I removed .iml file. I fixed it this way:

  1. Closed IntelliJ IDEA
  2. Deleted .idea dir which was in my project dir.
  3. I reopened IntelliJ IDEA and .idea dir and also .iml was after a while automatically recreated by IntelliJ.

Fixed for me.

Solution 3:

For Maven projects:

  • Open the Maven Tool Window (for intellij users - right click on the project name and scroll down to the bottom to find the maven tool window) (indicated with this icon: enter image description here)
  • Click the Reimport All Maven Projects button (indicated with this icon: Reimport All Maven Projects).

Instructions: https://www.jetbrains.com/help/idea/maven-projects-tool-window.html

Solution 4:

enter image description here

If you delete the *.iml file and the .idea folder and close the IntelliJ, it will be re-generated when you open the project again in the next time.

Solution 5:

I also removed .iml file by accident. Here is how it was resolved:

  1. Closed IntelliJ IDEA
  2. Reopened program
  3. Selected Import Project -> Create project from existing sources
  4. Clicked Next. It created correct project configuration for me.