Tuesday, September 6, 2011

How to run Java Application on NetBeans IDE

How to run an application on NetBeans IDE.

To write your first program, you'll need:
  1. The Java SE Development Kit 6 (JDK 6)
  2. The NetBeans IDE

Create an IDE Project

To create an IDE project:
  1. Launch the NetBeans IDE.
    • On Microsoft Windows systems, you can use the NetBeans IDE item in the Start menu.
    • On Solaris OS and Linux systems, you execute the IDE launcher script by navigating to the IDE's bin directory and typing ./netbeans.
    • On Mac OS X systems, click the NetBeans IDE application icon.
  2. In the NetBeans IDE, choose File | New Project.
  3. In the New Project wizard, expand the General category and select Java Application.
  4. In the Name and Location page of the wizard, do the following:
    • In the Project Name field, type Hello World App.
    • In the Create Main Class field, type helloworldapp.HelloWorldApp.
    • Leave the Set as Main Project checkbox selected.
  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.

Add Code to the Generated Source File

When you created this project, you left the Create Main Class checkbox selected in the New Project wizard. The IDE has therefore created a skeleton class for you. 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!"); // Display the string.
 
 
Optionally, you can replace these four lines of generated code:
/**
 *
 * @author 
 */
 
With these lines:

/**
 * The HelloWorldApp class implements an application that
 * Simply prints "Hello World!" to standard output.
 */
 
These four lines are a code comment and do not affect how the program runs

Save your changes by choosing File | Save.

The file should look something like the following:
/*
 * HelloWorldApp.java
 *
 * Created on February 5, 2011, 6:43 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
 
package helloworldapp;
 
/**
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
 */
 
public class HelloWorldApp {
    
    /** Creates a new instance of HelloWorldApp */
    public HelloWorldApp() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
    
}

Compile the Source File into a .class File

To compile your source file, choose Build | Build Main Project from the IDE's main menu.
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. You double-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
Now that you have built the project, you can run your program.

Run the Program

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

No comments: