Monday, July 23, 2012

Hello World Eclipse Program

This post will aim to provide support to beginners who want to start with Java and Eclipse. Here in this post We will write a simple program to read user input and print an output to the console in Eclipse.

Before proceeding further users are requested to go through Environment Setup page to ensure they have right kind of setup to proceed further.

Contents:

Create Eclipse Java Project

Go to Eclipse installation folder and open Eclipse.

Click on File -> New ... -> Java Project

Fill in the project Details.
1. Give project name as HelloEclipse.
2. Select Project Specific Jre & select Jre6
3. Create Seperate folders for source & class files.

Click on Finish.

Create Java Class with Main function

Right Click on Source folder -> New -> Class

Give Package name as com.java.automation
Name: HelloEclipse
Check public static void main(String[] args)
Click Finish

Hello Eclipse Code


/**
 * @author Raghu Kiran
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Feel Free to use/modify/distribute this code without removing this note.
 * No warranty or Support will be provided what so ever.
 * 
 */
package com.java.automation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * A simple program to read user input and print out on Screen.
 */
public class HelloEclipse {

 /**
  * @param args
  * This Programs reads your name as input in Eclipse Console.
  * prints it out with a welcome message.
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  String name = "";

  try {
   // Request user to Enter name
   System.out.print("Enter your Name: ");

   // Read user name
   BufferedReader reader =
             new BufferedReader(new InputStreamReader(System.in));
   name = reader.readLine();

   // Print out the user name and welcome Mesage.
   System.out.println("Hello, " + name + ". Welcome to Eclipse!!!");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   // Catch Any un-expected error while reading input log and exit!
   System.err.println("Error while reading input from the user, Below is stack trace... ");
   e.printStackTrace();
   return;
  }
 }
}



Enable Eclipse Console View

Enable Eclipse Console view.
you can give inputs to programs and see console outputs in this window.

Run your First Eclipse Program

Right click on the HelloEclipse.java file
Run As
Java Application.


Be the first to comment on this post

Post a Comment