[netbeans] Netbeans - class does not have a main method

My program is just a simple System.out.println(""); But netbeans cannot find the main method. Is netbeans 6.7.1 conflict with WIN7? Any possible error?

This question is related to netbeans

The answer is


Sometimes passing parameters in the main method causes this problem eg. public static void main(String[] args,int a). If you declare the variable outside the main method, it might help :)


This destroyed me for a while.... I knew that there HAD to be an easier way with a world class IDE like Netbeans.

The easiest method is to press Shift+F11 (Clean and Build Project), then hit F6 to run it.

It refreshes Netbeans appropriately and finds your main without all the manual labor; and if you have multiple mains, it will give you the option to select the correct one.


  1. Check for correct method declaration

public static void main(String [ ] args)

  1. Check netbeans project properties in Run > main Class

It was most likely that you capitalized 'm' in 'main' to 'Main'

This happened to me this instant but I fixed it thanks to the various source code examples given by all those that responded. Thank you.


My situation was different I believe because non of the above solutions di work for me. Let me share my situation.

  1. I am importing an existing project (NewProject->Java->Import Existing Projects)
  2. I name the project to xyz. The 'main' function exists in Main.class.
  3. I try to run the code I modified in the main function but the error pops out. I tried the shift_f6, specifically rebuild. Nothing works.

    Solution: I took the project properties and saw the 'Source Package Folder' mappings in the Sources branch was blank. I mapped it and voila it worked.

Now anyone might think that was very silly of me. Although I am new to Java and Netbeans this is not the first time I am importing sample projects and I saw all of them had the properties similar. The only difference I saw was that the main class was not having the name as the project which I believe is a java convention. I am using JDK7u51 (latest till date), is it causing the issue? I have no idea. But I am happy the project is running fine now.


Exceute the program by pressing SHIFT+F6, instead of clicking the RUN button on the window. This might be silly, bt the error main class not found is not occurring, the project is executing well...


Make sure it is

public static void main(String[] argv)

No other signature will do.


This happens when you move your main class location manually because Netbeans doesn't refresh one of its property files. Open nbproject/project.properties and change the value of main.class to the correct package location.


I had this issue as well (Error: Could not find or load main class test.Test). I'll explain this relatively basically since I know I would have appreciated someone doing that when I was looking for my answer.

When I right-clicked on the project (left hand side of the screen unless you got rid of the projects tab) and went to properties and then run, the main class had the projectname.classname, which is what confused me. For example, I created a project "test" to test this out, and when I went to

(right-click) test or Source Packages -> properties -> run -> main class

had Test.test in that field, which is what the problem was. the class is test, not Test.test, so I clicked browse to its right, and the only thing in the list to select from was test, and when I selected that and tried rerunning it, it finally worked.

Additionally, I found that when you create a new project in Netbeans, one of the things it originally gives you (in my case of the project named test) is package test;. If you are having this problem, then like me, you probably originally got rid of that line seeing it as just another line of code you didn't need. That line of code is what enabled your main class which in my case was Test.test to find the main class *test from that.


in Project window right click on your project and select properties go to Run and set Main Class ( you can brows it) . this manual work if you have static main in some class :

public class Someclass
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        //your code
    }
}

Netbeans doesn't have any conflict with W7 and you can use version 6.8 .


If you named your class with the keyword in Java, your program wouldn't be recognized that it had the main method.


While this may be an old question, the problem is still occurring these days, and the exact question is still not answered properly.

It is important to note that some projects have multiple classes with a main method.

In my case, I could run the project via the main class, but I could not run a particular other class that had a main method. The only thing that helped me was refactoring the class and renaming it. I've tried:

  • restart NetBeans
  • re-open the project
  • clear NetBeans cache
  • delete the file and create a new one with same name and contents
  • delete the file and create a new one with same name but very simple contents with only main method and print out message
  • rename the class (refactor) so a temp name and back
  • delete the project and create a new one with the same sources

The only thing that let me run this class is renaming it permanently. I think this must be some kind of a NetBeans bug.

Edit: Another thing that did help was completely uninstall Netbeans, wipe cache and any configuration files. It so happened that a newer Netbeans version was available so I installed it. But the old one would have probably worked too.