[java] Can't choose class as main class in IntelliJ

I have a Java project in IntelliJ to which I just added a bunch of files in a nested folder hierarchy. Many of these files are tests and include the main method, so I should be able to run them. However I cannot figure out how to do this.

I'm new to IntelliJ and previously files have shown up in the project hierarchy with the symbol that corresponds to "Java class that contains declaration of the main() method." but in this scenario they show up with the symbol corresponding to "Java class located out of the source root."

So how do I compile and run these files?

This question is related to java intellij-idea

The answer is


Select the folder containing the package tree of these classes, right-click and choose "Mark Directory as -> Source Root"


Here is the complete procedure for IDEA IntelliJ 2019.3:

  1. File > Project Structure

  2. Under Project Settings > Modules

  3. Under 'Sources' tab, right-click on 'src' folder and select 'Sources'.

  4. Apply changes.