38

Trying to create a Run configuration in Intellij For a Java application and get a warning that Class not found in module and it doesn't create the Run configuration. This only happened once I added the project to BitBucket. Before that it ran fine.

user840930
  • 5,214
  • 21
  • 65
  • 94

9 Answers9

70

None of the above worked, I had to do:

File -> Invalidate Caches/ Restart

Upon restart it re-indexed all files and everything worked again as expected.

Blackbam
  • 17,496
  • 26
  • 97
  • 150
Martin Schüller
  • 904
  • 1
  • 9
  • 7
34

The way I fixed the problem was to remove the existing Content Root from the project and adding a new one. For some reason, it looks like it got corrupted.

File->Project Structure under Modules : then on the far right side of the screen where it says Add Content Root, remove the existing Content Root and add it back again pointing to the correct location.

user840930
  • 5,214
  • 21
  • 65
  • 94
  • This happened to me while importing a scala maven project, and intellij was dumb to not recognise the App class. – Yogesh Kumar Gupta Jun 19 '20 at 18:26
  • 1
    This was still a working solution for me on IntelliJ IDEA 2020. I suspect this happened to my run configs after an IntelliJ minor version update. – Jim Tough Apr 07 '21 at 14:10
  • Tks, after deleting .idea from my git repo, the project stopped compilling (of course...), by setting the 'Sources folder' under Modules, it worked again – Girdacio Pereira Apr 25 '22 at 19:46
6

I'll explain a little bit more detailed version of the Accepted answer.

  1. Open Project structure window (Using Ctrl+Shift+Alt+S shortkey or From File -> Project structure option in the IDE menu)

  2. From there, select modules tab and remove the current configuration you have.

Project structure image

  1. Import module again.

Import module

  1. Select the root of your module (The old .iml file should be in that folder)

Select the right folder

  1. A dialog will popup. Select default options and finish.

  2. Apply the modifications by clicking OK.

Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
4

I've struggled with the same problem on my Kotlin project. The solution that works for me:

  1. Right mouse click on src/main/kotlin/Main.kt
  2. "Override file type"
  3. Choose "Kotlin" file type in the "Override file type" menu
  4. In the "Run/Debug Configuration" set "Main Class" as "MainKt"
vireal01
  • 41
  • 3
  • Thank you! I had the same problem. I created a new Kotlin project from the Kotlin/Gradle template and added my own main.kt file (called as such) but until I did what you suggested above, got the same error about not being able to find the class in my own module. – Marc Fearby Jan 09 '23 at 10:32
  • This is the best answer that solved my issue direct, no frills. – clusterBuddy Aug 28 '23 at 22:30
2

My variant: Run -> Edit Configurations -> Configuration -> Environment -> Shorten Command Line Select "JAR Manifest".

Marianna
  • 21
  • 1
1

i deleted .iml file and reimported the project using maven.

saba
  • 332
  • 2
  • 14
1

i solved it by right clicking on "pom.xml" -> add as maven project

E. Maurer
  • 11
  • 1
  • Thanks, in version 2023.1 this options moved to "pom.xml" (right click) -> Maven -> Reload project. And it works! – Umy Apr 30 '23 at 10:00
0

set working directory to module root directory or $MODULE_WORKING_DIR$ in Run/Debug configurations settings to help Intellij to find classes.

prabhakaran
  • 323
  • 1
  • 11
-1

i had this issue and i found out that the play button on the left side the function is the answer and it solved the error:))