How To Solve Error: “No Main Class Found in NetBeans”

Have you ever been working with NetBeans and got a very frustrating error that says: “No Main Class Found”?  We are going to show you to how to solve that problem in this article.  But first we must start with some background so you can understand the process.

Let’s start with knowing what NetBeans actually is. So NetBeans is an integrated development environment (IDE) originally used for Java but in addition to Java development, it also has extensions for other languages like PHP, C++, C, HTML5, and JavaScript. It can be run on different operating systems like Windows, macOS, Linux, and Solaris. NetBeans based applications which include the NetBeans IDE and others can also be extended by third-party developers. In July 2006, under Sun’s Common Development and Distribution License (CDDL) the NetBeans IDE got licensed.  Recently NetBeans IDE and Platform were donated to the Apache Foundation by Oracle. In April 2019, it underwent a business development cycle and passed out as a top-level project so right now NetBeans is licensed under the Apache License 2.0.

NetBeans integrated development environment is an open source platform. NetBeans IDE supports the development of all Java application types which include Java SE, JavaFX Java ME, web, EJB, and mobile applications out of the box. The other features of IDE include Maven support, refactorings, an Ant-based project system, version control (which supports Git, CVS, Mercurial, Subversion and Clearcase).

NetBeans provide the facility to build software applications from a set of modular components also known as modules. These modules provide all the basic functions of the IDE. each of them have a well-defined function such as supporting different languages, editing or support for the CVS versioning system and SVN, it got all the components to support development of Java in a single download, allowing the user to start working immediately, but for other languages and new features NetBeans needs to be extended, new modules and packages need to be installed. For instance, Sun Java Studio Creator from Sun Microsystem, Sun Java Studio Enterprise, and Sun Studio are all based on the NetBeans IDE.

Main Method

Moving on to the main method, in Java language, a Java application can’t be built without a main method. A Java application is defined as a public Java class with a main() method.

  • The main() function acts as the starting point for any application. Whenever a program is executed main is the first function that is executed. All the other functions can be called by the main function. In a classic standard way, there is one main function which uses other instances of classes to function
  • Without the main() method, JVM will not execute the program.
  • Java main method return type is void i.e it doesn’t return anything, that’s why it’s return type is void. This has been done because once the main ends, the Java program terminates so return type should be null to keep the program simple and avoid any type of memory leaks
  • The signature of the method is always: public static void main(String[] args)

public: It is an access specifier. The public keyword is used before the main so that the Java virtual machine can identify the program’s point of execution. If the access specifier is other than public, that is private or protected, it will not be visible to JVM and the program wouldn’t know its execution point.

static: any function becomes static can be made static by using the keyword static. Static methods are the functions that can be run or invoked without the creation of any objects, so to call the main function, objects are not needed. It is necessary to call the main method without creating an object hence static is used

void: this specifies the run-type which is null. The compiler acknowledges that the method isn’t returning any kind of value.

main(): It is a default syntax that is already defined in the Java Virtual Machine. JVM calls this function to compile a program line by line and end the compilation after the function is completed. The main method can also be overloaded

String args[]: The main() method also accepts some kind of data input from the user. It accepts an array of strings through cmd line arguments. Command-line arguments are passed through the args parameter, which is an array of Strings.

Resolving the Error

Now we know that the main method is quite important to run a program on a JVM. Following are the possible solutions to overcome the error “Main class not found in NetBeans”:

The standard way of running the project with main:

  • Right-click on your project in the project explorer
  • Select ‘Properties’
  • Select ‘Run’
  • Make sure your main class is the one you want to be executed first when the program starts running
  • Make sure to use the fully qualified name i.e. mypackage.MyClass
  • Click OK
  • Run Project

If you just want to run the file, right-click on the class from the package explorer, and click Run File, or (Alt + R, F), or (Shift + F6)

Correct syntax signature of main:

  • Sometimes you often face a problem where NetBeans doesn’t find a class when you’re browsing from “main classes dialog window”
  • It could be that your main method does have the proper signature. e.g you forgot the public access specifier
  • The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above
  • Args: You can name the argument anything you want, but most commonly used convention is “argv” or “args”

Specifying the main class:

  • Make sure that in the project properties, under the run tab you’ve specified your main class.
  • Furthermore, to avoid the issue by making sure to check the main class in the properties, specifying it would help resolve the issue.

Memory/Cache SPACE ERROR:

  • Sometimes due to out of memory space error, NetBeans is unable to load or find the main class
  • RightClick on the project node and go to Set configuration
  • Select the main class for your application
  • Then clean and build

If you have tried this and still it is not working then:

  • Clean the cache by removing index file from the cache folder

Go to Home/NetBeans/nb/var/cache and delete the Cache folder. Then open the NetBeans IDE again and run the project

If things still don’t work, then try the following steps:

  1. Select the project from the project explorer
  2. Select Run from the Menu Bar

Select Compile

About the author

Zeeman Memon

Hi there! I'm a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.