28

I'm new to IntelliJ IDEA and I would like to run a simple Maven Quickstart project using it.

I followed all the instructions, the project was sucessfully built. But when I try to compile and run it , the Run button is not activated.

It looks like IntelliJ IDEA couldn't figure out where the main class is.

This is a picture of the project's hierarchy.

enter image description here

What is the problem ? And how can I fix it ?

Thanks !

user1885868
  • 1,063
  • 3
  • 17
  • 31
  • Try refreshing the Maven project in the Maven Projects tool window, if it doesn't help, refer to [this answer](http://stackoverflow.com/a/42427510/104891) for diagnostics. – CrazyCoder Mar 01 '17 at 19:17
  • How exactly did you create the project? Is your JDK setup correct? If you have a valid jdk then right click to java folder and Mark Directory as... and then Source Root. hope it works – hevi Mar 01 '17 at 19:20
  • @hevi check my comment on CoDel's comment below – user1885868 Mar 01 '17 at 19:22
  • @CrazyCoder it worked ! Can you post it as an answer ? – user1885868 Mar 01 '17 at 19:24

7 Answers7

22

Refresh the Maven project in the Maven Projects tool window (Reimport All Maven Projects), if it doesn't help, refer to this answer for diagnostics.

Reimport

The issue is that your source roots were not configured correctly from the Maven model for some reason and the .java file appears in a plain directory instead of the Source root.

You can tell that by the color of the folders and by the icon of the file.

Community
  • 1
  • 1
CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
11

You need to create a Java Project.

File->New->Project->Java->Next->Next->Finish

Then go to the top and do:

Right click parent folder of project->Add Framework Support->Click Maven->Ok

Now Maven should be added to your project and also you should be able to run it

If this doesn't allow you to run the project, click the little drop down box to the left of the grayed out run button and click Edit Configurations.

Then click the + and click Application and set your Main class to the class that has the main method in it, and then it should allow you to run the application.

Hope that helps!

LJP1203
  • 115
  • 2
  • 11
  • I have already tried that and it didn't accept my Main class. – user1885868 Mar 01 '17 at 19:30
  • 1
    Interesting I just reproduced your exact code and mine worked perfectly fine. The above accepted answer should help you out. – LJP1203 Mar 01 '17 at 19:38
  • Thanks, It worked for me, just by default IntelliJ set java version to 1.5, to change go to File-> Settings, build,execution,deployment->Compiler->Java Compiler – Juan Rojas Oct 30 '19 at 17:18
2

Seems like your java directory is not marked as Source root. Right click on it and click Mark directory as -> Source root

GreyGoblin
  • 129
  • 5
1

Have you imported the project as a maven projet ? If yes, click on the body of your fonction and then do CTRL+MAJ+F10 or MAJ+F10

CoDel
  • 173
  • 2
  • 15
1

My issue was that I was looking for the equivalent of calling "mvnw" from the console/terminal.

.mnvw (linux) OR mvnw (windows)

I didn't know where this was in IntelliJ. That's how I interpret part of the question that was asked.

The solution wasn't obvious to me so I wanted to share it.

Conceptually, you want to run the default maven configuration (not a specific lifecycle stage). Therefore you need to select the root entry in the Maven panel:

Select Root Entry in Maven Panel

Then you can play the green play button on the panel:

Run Maven Build

Alternatively, you can right click and run the maven build from the context menu.

Run from Context Menu

Luke Machowski
  • 3,983
  • 2
  • 31
  • 28
1

If maven not visible top-right side then maven plugins not avaiable in your intellij IDE.

Follow below steps: click settings icon(top-right side)->select plugin->click on checkbox at maven option->click apply button then ok button.

After few minutes maven able to see in you IDE.

0

You need to add to project e.g. Jetty or Tomcat (compile to WAR & run Tomcat server with this war).

If you will have Jetty maven plugin you will be able to run app via mvn jetty:run (Add this to Maven run configuration)

hya
  • 1,708
  • 2
  • 15
  • 22