Saturday, March 22, 2008

Hello World from Netbeans IDE 6.1 Beta

The easiest way to check how things work in any IDE is to write a bare minimum program which is complete in its own sense. A program which can be compiled and which can show some output. This makes the first Hello World program in Netbeans IDE 6.1 Beta important . This program will also help me navigate many features of Netbeans IDE in the process.

I must highlight here that the Netbeans IDE has many good tutorials available on internet . These tutorials teach us any thing which can be done on Netbeans IDE and in Java. I liked the tutorials by Sang Shin on his Java Passion site. But there is a small insect inside me who wants to explore things on its own. To click here and there and see what happens. It wants to apply its past common sense and knowledge to the new softwares and check whether he can work with it and get the thing done by itself. This is just like seeing a new model of Car and trying it out with a thought , so what it is a Car and there will be some gears and an accelerator whats the big deal.

As with any other software there is a File menu and it also has an option of New Project. I must start from this option for my new Hello World program.

As expected Netbeans started a wizard for the initialization of the new project. As soon as you can see steps on the left bar and a next button at the bottom you can identify this is a wizard which will guide me through the process of creating new project. This is just like finding a guide while traveling the world.

From Categories list I chose Java and from projects list I chose Java Application. This is not because I know much about them but because those are the options wizard presented as defaults. When I have already begun the journey on my own, it is interesting to see where the wizard leads me ahead.

On the next page there are text fields I must fill. Here I am very particular. I have my own taste of what should be the project name and what shall be the package name. So I discarded the defaults provided by Netbeans New Project Wizard. I chose to have a good package name as com.mycompany.person for my PersonExample project. The class which will contain main method will be created by wizard probably hence it is asking me the name of my main class so I procided com.mycompany.person.PersonExample

I usually write the main method in any project in a separate class and do not mix it with any other class which is used in the project. many books I have seen present the main method in some of the used classes as it is perfectly valid in Java to have main method any where in any class. So when I have a project with PersonExample as name I prefer to keep the main method in a class named PersonExample and keep nothing else in this class. Thus this class PersonExample become the launcher of the program and nothing else than that.

When I said next the wizard showed a progress bar on the left bottom part of the window telling me that it needs to do some initialization and code generation probably so it can present me the project with some items pre-created.

And here was the wizard done with its magic. Now I can see a new project open in the main window of the Netbeans IDE. The projects panel was showing the structure of the project with one Java file It has also created the desired package name for me. The editor pabe on the right panel was showing the class source code. The wizard has managed to write the whole class for me with the main method with all the default things which I will need in my program.

I know how to print Hello World in Java. All I have to do is just type System.out.println("Hello World"); and this is enough to print the message on the console. As soon as I started typing the Sys word the IDE started helping me write the things by suggesting possible completion of the word. Notepad cannot certainly do this to me. I love IDEs for this intelligence they have.

I completed the program by writing the single System.out statement and now was the time to compile my java source file and see the output. Let me check what Netbeans has done with the files on my hard disk. I opened the folder where the project was created.

Netbeans has created the required files in src folder and there is also a nbproject folder. The contents of nbproject folder says that they are some configuration files Netbeans maintain for the information about the project. There is a build.xml file in the project folder. Netbeans help files say that Netbeans use Ant as the build system. It also promises that I will not have to learn Ant to get the program compiled and it will all be done by Netbeans IDE automatically, and that means the IDE will use the Ant program internally.

I opened the build menu and chose the Build Main Project option. I got this tip obviously from the Netbeans IDE help page. The help is available on the Help Menu. I will say that this help is not much friendly and it will not guide you like a teacher, rather it is like reference. But here come the gurus in Java like Sang Shin who provide us simple tutorials for learning Java and Netbeans.

Then I clicked the Play button on the tool bar which was begging to be clicked looking towards me. I clicked the Play button and there was a output window presented to me with the output of my program. A Hello World message indeed. Good. I am Happy.

I can also see that the Netbeans IDE has also created a dist folder in my project folder. This dist folder contains the PersonExample.jar file. I know that Java programs can be distributed through a JAR file. Java Archive to be specific. This file accompanied with a read me file which has instructions about how to use this JAR file and how to run the program outside Netbeans IDE also.

After all this was a Java program and certainly not tied to the IDE it is developed. This was great to get the JAR file automatically without any effort to create it on the command prompt using some JAR command.

According to what Readme file said I opened the command prompt in my Windows XP machine. I navigated to the folder where this JAR file was located. I typed the exact command as was instructed.

The same output message was displayed on the console. This proves that I have written a complete program which can work independently and do not need help of any IDE any more. The journey for making this program was made happier by Netbeans IDE for me.

Object oriented programming is my passion. It will be fun checking how my classes and objects can be written in Netbeans and how easily I can make a big program from scratch in Netbeans. Till now the experience is smooth and I hope many more new features to learn and explore in my journey of exploring Netbeans IDE.

with regards

No comments:

Post a Comment