NetBeans 12.0: “Class does not have a main method” (but it does)

NetBeans 12.0

Jdk 15

I cannot launch my program, clicking the green run button doesn’t do anything, and right click + run file on MainFrame gives the error “Class does not have a main method”. But as you can see in the GIF below, it does have a main(String args[]).

Does anyone have any idea why this happens?

enter image description here

enter image description here

enter image description here

Answer

Initial suggestions:

  1. Make sure all files are saved first. If you try to run a program where the class containing the main method has not been saved, this can happen.

  2. Right-click on the project name (in the Projects explorer), then select Properties > Run – and then make sure the main class is selected there. Again, if you have not saved the files NetBeans may not be able to find the main method (yet).

It may also be worth checking whether you have any other classes in your project which also have a public static void main(String args[]) method.

If all else fails:

Clearing the NetBeans cache may help.

Go to Help > About, and note the location of the cache directory. Exit from NetBeans. Delete the Cache directory. Restart NetBeans.


WARNING

Please note what @skomisa points out in the comments:

NetBeans 12 does not support Java 15, and while it may work for a trivial project, there are still issues (as shown by the comments here). It would be reckless to use that combination for production code, and the IDE (as opposed to the compiler) may not handle new features in Java 15 appropriately.