In this tutorial we are going to create a standard application for all beginners of a language; “Hello World“.  This application will examine the basics of the Java language and give us the end result of printing “Hello World” to the console screen.  We will then take it one step further and ask the user for their name and when they press enter it will say hello to that user. We will use both eclipse and NetBeans for this tutorial to show both interfaces and give you a chance to decide which one will work best for your development needs.

To being we will start with opening up NetBeans and completing our Hello World application on this IDE and just transfer it over to the eclipse IDE to give you a feel for both.

After NetBeans is open we can then proceed to start a new project by clicking the the cube with the green plus (+) sign on the top right of the cube in thetop sidebar.  Once this is clicked we will be directed to the New Project page where it will list the categories of applications that you can make and under each category will be a subset of projects for Applications or Libraries you can make.

After we have chosen the Java Application under the Java Category we are then directed to the second page of the project creation page which will ask for the name of the Project, the location the project will be created in and if you you like to create a main class and set as your main project.  For the project name we will want HelloWorld and I have chosen my development but clearly yours will be different.  I have also chosen to check Create a Main Class and set this as our Main Project for now.

After this has been completed we can now press Finish and the IDE will take us to our newly created HelloWorld.java file (see above) that has been populated with a Main method and comments.  We can now begin to start coding our first application and to start we will need to insert import java.io.*; just under our “package helloworld;“. After that has been completed we need to put some code below into our main method so it looks something like this:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package helloworld;
import java.io.*;
/**
*
* @author BinaryWasteland
*/
public class HelloWorld {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Howdy World!");

String name = "";

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("What is your name user: ");
try
{
name = reader.readLine();
}
catch(IOException e)
{
System.out.println("Input/Output error!");
System.exit(1);
}

System.out.println("Howdy " + name);
}
}

When the code above is typed into the IDE you will notice a helper box that appears giving you all of the methods and functions for any variables or classes. This can be useful when you know what you want to do but don’t know the correct way to go about it off the top of your head.

In the above code we initialize a string variable named “name” to catch the name that the user will enter. To be able to accept the name from the user we will use the BufferedReader to initialize a variable so we can use the ReadLine feature. We will wrap reading in the users entry in a “try catch” so we can try reading in the line and if any errors occur we can present them to to user in a readable and formatted way. After the line has been successfully copied to the string variable “name” we will want to output a welcome or hello message to them.

After you have finished either typing about the code above or copy pasted it into your own document we can then run the Hello World Program and it should look something like this:

After this is complete we now have a working Hello World application in out NetBeans IDE. We now want to explore using the eclipse IDE and get a feel for that development environment. To do this we will first start by opening up our eclipse environment and we will be asked to choose our workspace. After that is chosen you will be presented with an image like the one below:

Once eclipse is up and running we can now begin making our “Hello World” application by creating a new project. To do this we need to click on the icon in the toolbar that looks like a screen with a yellow plus sign on the top right of it. After this is pressed a screen will appear that allows you to check which type of project you would like to create.

In this instance we would want a Java Project. After selecting this we can then proceed to click Next and proceed to enter the information needed for the Project to be made. The Project Name should be HelloWorld and we want to use the Default location to save the project in. We can then keep everything else the same and the screen should now look like this:

The second page does not require modification so we can proceed to press the Next button but it will look like this:

After that is done we will be presented with the main screen of the eclipse environment but the package explorer on the left pane now has our project created in it.

We need to expand the project and right click on the package folder labels “src” and then click New -> Class. After we have selected Class a new screen will appear that will prompt us for the package name to create the file in. I have chosen to name it HelloWorld in this instance but you can name it whatever you would like. We also need to give the class a name which I have also made to be HelloWorld. We will want to check the box that says “public static void main(String[] args)” and we can optionally choose to generate comments which would generate your name as the author in the comments. After this is all done your screen should look like this:

After this is completed we can proceed to press the “Finish” button and our class will be created. After this is done we can insert the code from above into this IDE and class and press the play button with a green circle around it in the menu bar. This will execute the code above and make the output look something like this:

We have created a Hello World project in NetBeans and eclipse and given some time to each so you know which one you would like to develop in more( or continue on this path using both). With this tutorial complete you should know how to successfully start up the IDE of your choosing, store data to a String variable and output text onto the screen based on user input. If you can’t wait for the next tutorial check out these great reads. Until the next tutorial, Happy Hacking!

<-- Back to A Tutorial Series
Share This