134

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?

C. E.
  • 10,297
  • 10
  • 53
  • 77
  • 2
    For anybody coming here with the same issue but no "source root" problem - the runnability and "main class" indicators disappeared when I accidentally imported `com.sun.org.apache.xpath.internal.operations.String` - still can't figure out _why_ it happened! (IDEA 2018.1, JDK 8, Maven) – Janaka Bandara Apr 19 '20 at 07:57
  • It's annoying because In the configuration definition panel there is a dialog for Choosing the main class with a tab listing all the project files, Why one of those with a main method in it cannot just be selected, I find very unintuative. – Andrew S Mar 25 '22 at 01:42

6 Answers6

184

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

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • This did what I asked for, however now it can't resolve the import statements. Other errors appeared as well. Ideas? – C. E. Feb 19 '14 at 15:11
  • For example it asked me to prepend methods with "odeToJava.modules." and then complained that it could not resolve odeToJava. – C. E. Feb 19 '14 at 15:16
  • How do you get to the menu that lets you do this though? You view the project directories and files in Project view (alt + 1) then right click on folder following OP's instructions. – James T. May 22 '18 at 21:48
  • I've done this but it only allows me to select the grade files and not the main JAVA runner. – Oliver Dixon Mar 17 '20 at 11:18
  • This was needed after importing a gradle project after the main method of a java class did not have the run icon in the gutter next to the main method. – basswaves Nov 13 '20 at 20:30
  • I was almost at the verge of uninstalling intellij Thanks this solve my issue It still not showing me the main class inside the package (configuration) i typed it manually which solve my issue – Azhar Uddin Sheikh Aug 25 '21 at 14:38
73

The documentation you linked actually has the answer in the link associated with the "Java class located out of the source root." Configure your source and test roots and it should work.

https://www.jetbrains.com/idea/webhelp/configuring-content-roots.html

Since you stated that these are tests you should probably go with them marked as Test Source Root instead of Source Root.

trappski
  • 1,066
  • 10
  • 22
  • 1
    Make sure you set up your module dependencies properly. Mark the module in the project structure and 'F4' is the default key to open module settings (atleast in Linux). (Or right click and choose 'Open Module Settings'). Hopefully this here can help a bit further: https://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html – trappski Feb 19 '14 at 15:46
  • Ah, yes, dependencies solved my problem in the end. ty. – C. E. Feb 19 '14 at 19:22
21

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.

Ashwin
  • 7,277
  • 1
  • 48
  • 70
6

Sometimes under the current automatically configured sources root you need to specify the module classpath:

  1. Click Run -> Edit Configurations
  2. Select the project you can't find the main class for
  3. Under the Use classpath of module pulldown see if there is a sub directory that has your main class under it. If so, select it.
  4. try to configure the main class again.
user15911102
  • 61
  • 1
  • 1
6

I also faced another issue: the main method has to have arguments

This way is incorrect, Intellij will not let you select main method.

public class yourClass() {

  public static void main() {

This way is CORRECT and Intellij will let you select it

public class yourClass() {

  public static void main(String[] args) {
Ton
  • 101
  • 1
  • 5
0

I'm using Eclipse but it will be something similar like this.

Right click on project and select something similar to 'Maven' >> 'Reload from maven'.

Intelij should be able to understand project structure from Maven pom file and will select main class automatically.

Raju Penumatsa
  • 403
  • 2
  • 5
  • 12