Installing java SDK and writing first java program

Installing and using Java

Install the java SDK from below location and follow the installation instructions,

Setting environment variable for Java

Environmental variable is a variable that describes the operating environment of the process.
Common environment variables describe the home directory, command search path etc.


JAVA_HOME is a environment variable (in Unix terminologies), or a PATH variable (in Windows terminology) which contains the path of Java installation directory.

On window operating system follow the below steps,

1.Right click My Computer and select Properties.
2. On the Advanced tab, select Environment Variables, and then edit JAVA_HOME to point to where the JDK software is located.

or run the below from command prompt,

Set JAVA_HOME = <jdk-install-dir>

example, C:\Program Files\Java\jdk1.6.0_02.

On Unix operating system run the below command,

export JAVA_HOME=<jdk-install-dir>


Set PATH =%PATH%;%JAVA_HOME%\lib\tools.jar



Please note, changing the JAVA_HOME will reflect to path and class path both.

To test above settings, go to command prompt and type ‘javac’.It will display all the options of using javac.
If it says bad command or file name then check the path setting.

Writing first Java program,

A Java program file is an ordinary text file with .java as extension. Java source files are created in a plain text editor, or in an editor that can save files in plain ASCII without any formatting characters. It contains one or more class definitions. In Java all code must reside inside a class. Even the main method has to be in a class.



public class HelloWorld {

public static void main (String [] args)
System.out.println("Hello World !!");


This contains a main method, which is the starting point of execution of the program.
In this example main method prints “Hello World” text.
Type the above code in any editor e.g notepad and save it as, the name of the file must be the same as the name of the public class [i.e HelloWorld]. The extension must be .java While saving the file using the text editor, make sure that the file is saved with an extension “.java” and NOT as “.java.txt”.

There are few top-level elements that may appear in a file. None of these elements is required. If they are present, they must appear in the following order:
•Package Declaration (optional –only one)
•Import Statements (optional –any number)

Compiling a Program

To compile the program, Open a command prompt, Go to the directory in which you have saved your program execute the compiler javac, specifying the name of the source file on the command line.

e.g.  javac

The javac compiler creates a file called HelloWorld.class. This.classfile is nothing but the bytecode for the program. The bytecode is the intermediate representation of your program that contains instructions the Java interpreter will execute. Thus, the output of javac is not the code that can be directly executed.

Executing a Program

To actually run the program, you must use the Java interpreter, called java. To do so, pass the class name at the command-line:
e.g. java HelloWorld

When the program is run, you get the output as shown below,

Hello World !!

Let us see some best practices to be followed while writing Java Programs,

  • One .java file must contain only one class declaration.
  • The name of the file must always be same as the name of the class.
  • Stand alone Java program must have a public static void main defined.
  • Code should be adequately commented.
  • Must follow indentation and coding standards.

Leave a Reply

Your email address will not be published. Required fields are marked *