IntelliJ IDE | .iml File lost or deleted

JavaTomcatIntellij IdeaProject Structure

Java Problem Overview


Last week I was working on a Java EE project with IntelliJ IDE.

Today: As I started up my IntelliJ 2016.1 and it gave me the error:

Error Loading Project

Cannot load module FirstWebApp

As I looked at the details it said it could not find my FirstWebApp.iml file and so could not load my project.

I checked out my project structure in the Explorer and everything except the iml file were there. I also looked at the Recycle Bin but I couldn't find it there as well.

  • How can such a file just disappear?

  • How can I get my iml file back if its possible?

  • What do these iml files do?

I'm new in the Java World. If more information is needed just comment! :)

Java Solutions


Solution 1 - Java

*.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 - Java

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 - Java

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 - Java

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 - Java

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.

Solution 6 - Java

  1. delete .idea folder inside your project
  2. re import the project

it will again compile and build .idea folder for the configuration.

Solution 7 - Java

If you can't recover it via the most upvoted way, you could simply copy another .iml file, paste it into the package* that requires a .iml file, and rename the .iml file to the name of the package. This should work if it is the same type (in my case UTF-8).

*Copy the .iml file by going into package files, right-clicking the package name, and press paste.

You can accomplish this in the left-hand task bar. I have prepared a video demonstration (there's no audio).

Solution 8 - Java

The Gradle refresh did not work in my case. Several project folders were also missing, so I could not copy/paste new .iml files either.

However when I opened the project anew, choosing Delete Existing Project and Import, the project was able to recreate all the .iml files.

Solution 9 - Java

Create a new file with the project name and paste it. In case you are working with flutter.

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/lib" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
      <excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
      <excludeFolder url="file://$MODULE_DIR$/.idea" />
      <excludeFolder url="file://$MODULE_DIR$/.pub" />
      <excludeFolder url="file://$MODULE_DIR$/build" />
    </content>
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" name="Dart SDK" level="project" />
    <orderEntry type="library" name="Flutter Plugins" level="project" />
    <orderEntry type="library" name="Dart Packages" level="project" />
  </component>
</module>

Solution 10 - Java

I am using phpstorm and project with multiple repos.

My scenario was that, for reasons unspecified, i deleted "main" project directory (and cloned again). Which resulted next project directory to become main , i guess. And also old "main" to be removed from project view in phpstorm.

After trying to open newly cloned project and attach, it failed. New .iml file in attached newly cloned project was fine (although it reported failure), so i went into next projects .idea directory and found .iml file looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
  <component name="NewModuleRootManager">
    <content url="file://$MODULE_DIR$" />
    <orderEntry type="inheritedJdk" />
    <orderEntry type="module" module-name="Project1" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module" module-name="Project3" />
    <orderEntry type="module" module-name="Project4" />
    <orderEntry type="module" module-name="Project1" />
</component>
</module>

So i quit phpstorm, removed all Project1 entries from the second project iml file, restarted IDE, opened and attached newly cloned project1 again, and it was suddenly fine.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSunflowerToadTheOrbiterView Question on Stackoverflow
Solution 1 - JavadambrosView Answer on Stackoverflow
Solution 2 - JavatuizView Answer on Stackoverflow
Solution 3 - JavaDanArlView Answer on Stackoverflow
Solution 4 - JavaArefeView Answer on Stackoverflow
Solution 5 - JavavitaliisView Answer on Stackoverflow
Solution 6 - JavaVikramjeet SinghView Answer on Stackoverflow
Solution 7 - JavaSassyLlama BoiView Answer on Stackoverflow
Solution 8 - JavaSigurd Knarhøi JohannsenView Answer on Stackoverflow
Solution 9 - JavaAshish BairwaView Answer on Stackoverflow
Solution 10 - JavaAllar OlgoView Answer on Stackoverflow