Sunday, February 10, 2008

Running your First Java Program using Netbeans 6.0

Well if you have been following my blogs, then this one is third in the series. I was highly motivated to write this blog when a couple of first years told me that my previous blogs had enabled them to get started with the Java programming language and they egged me to show them the way to get started with the Netbeans IDE. While in the previous two blogs I showed how to run a simple Java program and then go about debugging them (using the windows notepad as the editor and the command prompt), this one will introduce you to the Netbeans IDE.

In computing, an integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, a compiler and/or interpreter, build automation tools, and (usually) a debugger.Typically an IDE is dedicated to a specific programming language, so as to provide a feature set which most closely matches the programming paradigms of the language. However, some multiple-language IDEs are in use, such as Eclipse, ActiveState Komodo, SlickEdit and recent versions of Microsoft Visual Studio.

NetBeans began in 1997 as Xelfi, a student project under the guidance of the Faculty of Mathematics and Physics at Charles University in Prague. A company was later formed around the project and produced commercial versions of the NetBeans IDE until it was bought by Sun Microsystems in 1999. Sun open-sourced the NetBeans IDE in June of the following year. The NetBeans community has since continued to grow, thanks to individuals and companies using and contributing to the project.

The NetBeans IDE is an open-source integrated development environment written entirely in Java using the NetBeans Platform. NetBeans IDE supports development of all Java application types (J2SE, web, EJB and mobile applications) out of the box. The current version is NetBeans IDE 6.0, which was released in December 2007.

I have tried to provide a very simple and quick introduction to the NetBeans IDE workflow by walking you through the creation of a simple "Hello World" Java program. Once you go through the same, you will have a general knowledge of how to create, build, and run applications in the IDE.

CHECKLIST

To write your first program, you'll need to have the following software installed on your system:

  • The J2SE Development Kit (JDK).
  • NetBeans IDE 6.0.

To create an IDE project follow these simple steps. I have tried to include as many screen shots to make the things as clear as possible.

  1. Start NetBeans IDE.
  2. In the IDE, choose File > New Project, as shown in the figure below.

  3. In the New Project wizard, expand the Java category and select Java Application as shown in the figure below. Then click Next.

    NetBeans IDE, New Project wizard, Choose Project page.

  4. In the Name and Location page of the wizard, do the following (as shown in the figure below):
    • In the Project Name field, type HelloWorldApp.
    • In the Create Main Class field, type helloworldapp.HelloWorldApp.
    • Leave the Set as Main Project checkbox selected.

    NetBeans IDE, New Project wizard, Name and Location page.

  5. Click Finish.

The project is created and opened in the IDE. You should see the following components:

  • The Projects window, which contains a tree view of the components of the project, including source files, libraries that your code depends on, and so on.
  • The Source Editor window with a file called HelloWorldApp open.
  • The Navigator window, which you can use to quickly navigate between elements within the selected class.

NetBeans IDE with the HelloWorldApp project open.

Because we left the Create Main Class checkbox selected in the New Project wizard, the IDE has created a skeleton class for us. You can add the "Hello World!" message to the skeleton code by replacing the line:

            // TODO code application logic here
with the line:
            System.out.println("Hello World!");

Save the change by choosing File > Save.

The file should look something like this:

/*
* HelloWorldApp.java
*
* Created on Feb 28, 2008, 7:56:01 PM
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package helloworldapp;

/**
*
* @author Rahul Mathur
*/
public class HelloWorldApp {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
}

}

COMPILING THE SOURCE FILE

To compile your source file, choose Build > Build Main Project from the IDE's main menu.

You can view the output of the build process by choosing Window > Output > Output.

The Output window opens and displays output similar to what you see in the following figure.

Output window showing results of building the HelloWorld project.

If the build output concludes with the statement BUILD SUCCESSFUL, congratulations! You have successfully compiled your program!

If the build output concludes with the statement BUILD FAILED, you probably have a syntax error in your code. Errors are reported in the Output window as hyper-linked text. Click such a hyper-link to navigate to the source of an error. You can then fix the error and once again choose Build > Build Main Project.

When you build the project, the bytecode file HelloWorldApp.class is generated. You can see where the new file is generated by opening the Files window and expanding the Hello World App/build/classes/helloworldapp node as shown in the following figure.

Files window, showing the generated .class file.

Now that you have built the project, you can run your program.

RUNNING THE SOURCE FILE

From the IDE's menu bar, choose Run > Run Main Project.

The next figure shows what you should now see.

The program prints Hello World! to the Output window (along with other output from the build script).

Congratulations! This is all that one needs to get started.

2 comments:

Unknown said...

hi
im trying to create a simply program like making a car race around tracks using netbeans the car doesnt have to be 3d it can be 2d i just want to no what tools i would need to use any help plz

MIJ said...

hey!!!!!!!!!

you saved me!!!!!!!!!

keep those great tutorials coming!

 
Page Views: