Running your Java Programs

You have a Main method in your code from the previous lesson. But it has no code inside of it. Let's add a line of code and then run it. We'll output some text to a console window. Add the following line to your main method:

public static void main( String[] args ) {

System.out.println("My First Project");


When you type the full stop after "System", NetBeans will try to help you by displaying a list of available options:

NetBeans Intellisense menu

Double click out to add it to your code, then type another full stop. Again, the list of options appears. Select println, this time. What this does is to print a line of text to the output screen. But you need to place your text between the round brackets of println. Your text needs to go between a pair of double quotes. Once you have your double quotes in place, type your text:

A Java println statement

Notice that the line ends in a semicolon. Each complete line of code in Java needs a semicolon at the end. Miss it out and the program won't compile.

OK, we can now go ahead and test this program out. First, though, save your work. You can click File > Save, or File > Save All. Or click the Save icon on the NetBeans toolbar.


Run the Program

When you run a programme in NetBeans, it will run in the Output window at the bottom of your screen, just underneath your code. This is so that you don't have to start a terminal or console window - the Output window IS the console.

There are various ways to run your programme in NetBeans. The easiest way is to press F6 on your Keyboard. You can also run programmes using the menus as the top of NetBeans. Locate the Run menu, then select Run Main Programme:

The Run Menu in NetBeans

You can also click the green arrow on the NetBeans toolbar:

The Run icon on the toolbar

Another way to run your programmes is from the Projects window. This will ensure that the right source code is being run. Simply right click your java source file in the projects window and you'll see a menu appear. Select Run File.

Running the programme from the NetBeans Projects window

Another way to run the code is to right click inside of the code area. In the image below, we've right-clicked just before the final curly bracket (again, we've chopped off some menu items):

Using one of the above methods, run your programme. You should see something happening in the Output window:

The Java Output window

The second line in the Output window above is our code: My First Project. You can quickly run it again by clicking the two green arrows in the top left of the Output window.

<-- Java Code Structure | Sharing your Java Programs -->

Back to the Java Contents Page