hello World – Java Using Eclipse

Background

Let us use Eclipse IDE to develop a simple Hello World application.

Eclipse IDE

Launch

Please launch Eclipse.

Develop

Outline

Here are the steps that we will take.

  1. Create new project
    • Select Wizard
      • Java Project
    • Project
      • Enter your project name
    • Location
      • Un-check “Use Default Location”
      • Please enter or select folder where project’s files will be stored
  2. Add package to Project
    • Why
      • Though not required it is best to bundle java classes into project
    • Source Folder
      • We will target our project’s Source folder
      • In our case helloWorld/src
    • Naming
      • By convention, packages are named to reflect their organization name
      • Our Organization name is learningintheopen.org
      • In Java, we turn the organization name around to get the package name
        • And, so end up with a base package name of org.learningintheopen
      • As we will be developing various packages, we will suffix this one with sample
  3. Add class to package
    • Let us add our lone class
    • We will name it HelloWorld
    • Modifiers
      • Please check public
        • Allows for public access
      • Final
        • Simple Class
        • Never to be overridden
    • Stubs
      • Please choose to add the main function to allow class’s accessibility
  4. Add a few steps to our HelloWorld class
    • Get System Environment Variable ( username )
      • Syntax
        • System.getenv([environment-variable])
      • Sample
        • strUsername = System.getenv(ENVIROMENT_VARIABLE_USERNAME);
  5. Build application
    • Auto-Build is enabled by default
      • And, so our project is constantly being built
    • To perform a clean build, please engage the menu and choose “Project/Clean…”
  6. Test application
    • To execute the application, please use the “Run” menu

 

Create New Project

Images

New Project – Select a wizard

 

New Java Project – Create a Java Project – 01

 

New Java Project – Create a Java Project – 02

New Java Project – Project Folder Selection – 01

New Java Project – Java Settings – 01

 

Add Package

Images

Image –  Add New Java Package

 

Image –  Package Added

 

 

Add Class to Package

Images

Image –  Add New Java Class – HelloWorld

 

Edit Source Code

Source Code

HelloWorld.java

/**
 * 
 */
package org.learningintheopen.sample;

/**
 * @author dadeniji
  */
public final class HelloWorld {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		sayHello();
	}
	
	public static void sayHello()
	{
		String ENVIROMENT_VARIABLE_USERNAME = "username";
		
		String strUsername;
		String strLog;
		
		strUsername = System.getenv(ENVIROMENT_VARIABLE_USERNAME);
		
		strLog = "Hello";
		
		if (strUsername != null)
		{
			strLog = strLog + " " + strUsername;
		}
		else
		{
			
			strLog = strLog + " " + "World!";
			
		}
		
		// TODO Auto-generated method stub
		 System.out.println(strLog);
	 
	}
	
}


 

Build

Images

Clean

Run

Images

Output

 

Console

Run Application From Command Line

Command

Syntax


java -cp [file-path] [package-name].[class-name]

Sample


java -cp bin org.learningintheopen.sample.HelloWorld

Output

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s