79

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! :)

vitaliis
  • 4,082
  • 5
  • 18
  • 40
SunflowerToadTheOrbiter
  • 1,285
  • 4
  • 15
  • 25
  • 3
    Not relevant to the original question, but, I just want to add: if you work with `git`, configure a `gitignore` so these files will not be pushed – Arefe Feb 16 '18 at 02:40

10 Answers10

98

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

Philip Rego
  • 552
  • 4
  • 20
  • 35
dambros
  • 4,252
  • 1
  • 23
  • 39
  • 1
    I think that button looks a little different now. I clicked on "refresh all gradle projects" and "execute gradle tasks" in the gradle window. Not sure which one generated the iml file, but I was fine after that. – hBrent Dec 20 '17 at 00:20
  • 1
    The settings are different now. I still don't see how to regenerate the impl file within a maven project. – DrB May 16 '18 at 11:34
  • @BeC I have just downloaded the latest CE edition of Intellij and for Maven projects the same procedure still regenerated the *.iml files. The + button isn't green anymore, but after clicking it and importing a module from existing sources it worked. Maybe you can try to remove the .idea/modules.xml file before adding the modules back to Intellij to regenerate everything. Let me know if it worked or not please. – dambros May 17 '18 at 14:45
  • There is no `sync project` button – IgorGanapolsky Oct 15 '19 at 21:04
  • I had a module generated by a 3rd-party app (that includes some code files that I'd rather edit in IntelliJ, therefore the inclusion in my IntelliJ project)… the other app 'helpfully' deleted the .iml file. I therefore discovered that a seemingly universal way to recover the .iml file is to remove the module from the project and then re-add it "from Existing Sources..." – JPGringo Mar 09 '20 at 17:41
  • @dambros what to do to solve this issue in Pycharm?? Any Idea?? – Prateek Gupta Apr 27 '21 at 20:37
  • 1
    @PrateekGupta what exactly is happening on Pycharm? I feel like the gradle problem doesn't apply to python – dambros May 26 '21 at 14:29
  • @dambros problem solved. Check out https://stackoverflow.com/q/67625251/13329963 – Prateek Gupta May 26 '21 at 14:55
  • Can it be that "not versioning" the file (putting it in .gitignore) is the reason people have this problem in the first place? At least for Python projects, I do not see that this file contains anything that is specific to one user or system, so why should it not be in GIT along with other versioned files from the .idea folder? – Florian Winter Dec 10 '21 at 09:26
86

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.

tuiz
  • 900
  • 7
  • 4
  • 1
    Thanks! after I had to do: File -> Invalidate Caches / Restart – Cohensius Mar 12 '20 at 08:20
  • this also fixed my problem! – Russell Lego Sep 15 '20 at 19:49
  • The other methods assumed maven or gradle. I am using IntelliJ without either of those and this worked for me. In fact when I used the Project Setings wizard to rename the project, it didn't update the .iml and doing this got me a new .iml file with the correct name. – steven smith Apr 28 '21 at 17:30
13

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

Satya
  • 15
  • 6
DanArl
  • 1,113
  • 8
  • 10
  • 1
    This is the only instruction that actually got my multi-layered Maven project being imported as Maven project/module. Thanks. – NuCradle Jan 27 '20 at 00:35
10

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.

Arefe
  • 11,321
  • 18
  • 114
  • 168
7

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.
vitaliis
  • 4,082
  • 5
  • 18
  • 40
7
  1. delete .idea folder inside your project
  2. re import the project

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

  • Easiest solution. And this works very well when your project is neither gradle based or maven based. Thanks! – Sushank Dahiwadkar Mar 30 '21 at 06:59
  • You will lose your window settings, run configurations, and other things when you do this. So keep track of that information so you can rebuild it. – Robert Casto Jan 25 '22 at 16:33
5

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).

halfer
  • 19,824
  • 17
  • 99
  • 186
2

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.

1

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>
Ashish Bairwa
  • 757
  • 1
  • 6
  • 19
  • What do you mean with `project name`? – IgorGanapolsky Oct 15 '19 at 21:09
  • 1
    @IgorGanapolsky try the project folder name: so _path/to/projects/my-project_ would mean that the .iml file in that folder should be called _my-project.iml_. that's at least what AndroidStudio created automatically when I import a project – rexxar May 08 '20 at 10:31
1

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.

Allar Olgo
  • 11
  • 1