IntelliJ IDE | .iml File lost or deleted
Asked Answered
G

10

80

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

Genotype answered 11/4, 2016 at 7:22 Comment(1)
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 pushedYacketyyak
C
101

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

Cacka answered 11/4, 2016 at 7:43 Comment(9)
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.Torch
The settings are different now. I still don't see how to regenerate the impl file within a maven project.Bogart
@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.Cacka
There is no sync project buttonRaffaello
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..."Coacher
@Cacka what to do to solve this issue in Pycharm?? Any Idea??Flamboyant
@PrateekGupta what exactly is happening on Pycharm? I feel like the gradle problem doesn't apply to pythonCacka
@Cacka problem solved. Check out https://mcmap.net/q/260682/-pycharm-2021-1-1-cannot-load-settings-from-file-error/13329963Flamboyant
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?Selfpity
W
87

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.

Wanids answered 12/11, 2019 at 15:13 Comment(3)
Thanks! after I had to do: File -> Invalidate Caches / RestartFlawy
this also fixed my problem!Eld
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.Beep
P
14

For Maven projects:

  • Open 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

Pendergast answered 11/11, 2019 at 18:31 Comment(1)
This is the only instruction that actually got my multi-layered Maven project being imported as Maven project/module. Thanks.Audacious
Y
11

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.

Yacketyyak answered 7/1, 2020 at 4:32 Comment(0)
O
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.
Oversubscribe answered 27/4, 2020 at 6:45 Comment(0)
S
7
  1. delete .idea folder inside your project
  2. re import the project

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

Sandstrom answered 21/7, 2020 at 10:7 Comment(2)
Easiest solution. And this works very well when your project is neither gradle based or maven based. Thanks!Humpy
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.Capitate
H
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).

Huntley answered 23/12, 2018 at 1:12 Comment(1)
I did this, but strangely, renaming the pasted .iml file also renames the copied .iml file.Gourde
R
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.

Remus answered 18/9, 2019 at 7:23 Comment(3)
There is Delete Existing Project?Raffaello
Yes, but it will not delete your project at such. It will close your project properly in IntelliJ and then reopen it from scratch. Usually you don't want to do that, since it takes longer to reopen the project this way. But when things don't work - it may help you.Afrit
It's worth mentioning, that doing this, will also clear your Run/Debug Configurations. So you will have to recreate those.Afrit
G
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>
Gripsack answered 9/9, 2019 at 17:27 Comment(2)
What do you mean with project name?Raffaello
@Raffaello 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 projectKaspar
D
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.

Dawson answered 29/7, 2020 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.