Running the main class

It is possible to compile and run the main class straight from NetBeans IDE. The primary reason for executing the main class is fairly obvious, that is, to test that modifications made in the software are actually what the coder(s) intended.

How to do it...

It is necessary to have a project in order to create a class, so if you are unsure how to do this, please check the Creating a Java project using Wizard recipe. However, when naming the project, enter RunMainClassApp.

With the main class open, enter the following line of code inside of the main() method:

System.out.println("Main class output");

Then right-click inside the Java editor of the Main.java file and select Run File.

How it works...

NetBeans will always show the Run File option for a Java class that contains the main method.

After Run File is selected, NetBeans will then compile and execute it. The output, if any, of the main class will be shown in the Output view.

For this example, the output is: Main class output

There's more...

Thinking that using the mouse is overkill? Or prefer running the entire project?

Run class shortcut

It is much faster to use some key combinations than to reach for the mouse every time. So, if changes need to be checked fast, just hit Shift+F6 and the class will be executed.

Run project shortcut

With a Java Desktop project there is a possibility to run the entire project. With this specific type of project, the Main class, the one created by NetBeans, is the one to execute.

It is possible to run the main project by right-clicking on the project name and selecting Run or pressing F6.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.114.132