Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't choose class as main class in IntelliJ

People also ask

How do I create a main Java class in IntelliJ?

To create a new class, press ⌘N on MacOS or Alt+Insert on Windows or Linux on the directory in the Project Window, and select “Java class”. When creating a new class, we can type the whole package path, separated by dots, followed by the class name, and IntelliJ IDEA will create the package and the Java file.

Can you make a class in main?

The main() method can appear in any class that is part of an application, but if the application is a complex containing multiple files, it is common to create a separate class just for main(). The main class can have any name, although typically it will just be called "Main".

How do I get to main method in IntelliJ?

We can type a short sequence, like psvm , and have IntelliJ IDEA generate things like the public static void main method. We can even create our own live templates for code snippets we commonly use. If we put the cursor inside the main method, we can run it by using ⌃⇧R, or Ctrl+Shift+F10 on Windows.


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


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.


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.


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.

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) {