Your first Java Program

Before going into more details about Java, let’s see how to setup Java and how to handle a Java program.

Make your computer ready

Before you compile and run a Java program you must have Java Development Kit installed on your computer. In short Java Development Kit is JDK. Currently latest version of JDK is 8. This is Java Standard Edition (SE) version 8.

To download JDK click here and download JDK. After downloading install JDK as per your operating system. I will explain here installation for Windows Operating System.

  • Default path for Java installation is ‘C:\Program Files\Java\'(For 64 bit machine).
  • After installation, open My Computer properties by right clicking on My Computer.
  • Select Advanced System Settings link on left hand side, it opens advanced settings dialog box.
  • Click on Environment Variables button. It opens Environment Variables dialog box.
  • Under System variables section find a variable called ‘path’.
  • If it is present select edit or not present select new.
  • Make sure Variable Name field is ‘path’.
  • In Variable Value field provide bin folder of your JDK. By default for JDK 8 it is ‘C:\Program Files\Java\jdk1.8.0_77\bin’
  • If some content is present in Variable Value field, at end of line give semicolon ‘;’ and paste JDK bin folder path.

Testing Java Installation

After installation of Java and setting Environment Variable, we need to confirm its working. To do this perform following actions,

  • Open Windows Command Prompt. (Windows -> Accessories -> Command Prompt)
  • Type ‘javac -version’
  • Output should be the Java version you have installed. In my case it is ‘javac 1.8.0_60’
  • If you able see Java version then you have successfully installed JDK.
  • Or else try to install and set environment variables as per instructions.

First Java Program

To write a Java program we can use any text editor. Every operating system comes with a default text editor such notepad for windows. Now open your favourite text editor and type the following code. And save it in some location (which you remember) and name it

/*This is a simple Java program.
Call this file
public class KH_FirstProgram{

	// A Java program begins with a call to main().
	public static void main(String args[]) {
		System.out.println("I am enjoying Java.");

Download the code Run the code

Compiling and Executing

To compile and run you can use our inbuilt sandbox, you just need to click Run the Code link and to download click Download the code link.

But to understand how to manually compile and execute, open command prompt and go to folder where you have saved your java file. Then type below command.


If it returns to command prompt without any errors, it means your program compiled successfully. Now open folder where you have saved your java file and look at files in it. You will see a new file named KH_FirstProgram.class is created. This is class file created by Java compiler. This file is only understood by Java Runtime Environment (JRE) and it is independent of operating system. We will learn about class files in next lesson. Now to execute our first program type below command in command prompt.

java KH_FirstProgram

This command executes a class file with name KH_FirstProgram and generates output. If everything goes as per plan, you should see below output.

I am enjoying Java.

Congratulations…you have successfully wrote and executed your first Java program.

Break down our first program

This is a simple Java program.
Call this file

This is called comment. Like other programming languages Java also provides opportunity us to write extra information in source file. Writing comments are very much important in understanding our program. Anything between /* and */ is a comment and this will be ignored by compiler. You can write any number of lines that’s why it is also called as multiline comment.

public class KH_FirstProgram

This line of code defines a class ‘KH_FirstProgram’ using class keyword. public keyword is used to restrict access of our class ‘KH_FirstProgram’. We will learn more about these keywords and classes in later lessons.

// A Java program begins with a call to main().

This also a comment but we can write only single line. Hence it is called as single line comment. This type comment should start with //.

public static void main(String args[])

Just forget about our small program for some time and imagine you are writing a big software which contains 100 Java files in it. Then how the system knows from which Java file software execution should start? That is there should be a single entry point for our software. This is provided by main function. main function should be present in only one Java files out of 100 java files. When program starting its execution JRE finds for main function and starts executing from it. If JRE did not find then it throughs an error. For now think the other keywords public, static and void are also participate in telling JRE about entry point of our program. We will discuss about them in later sessions. The last one String args[] holds any information we passing while executing out program. We will learn about it in later lessons.

System.out.println("I am enjoying Java.");

You might have already guessed what this line of code is doing. Yes, it is printing output to console window. This is inbuilt function to Java used to display output. We will discuss more about System.out in later lessons, but for now just remember that this complete line is used to print output.

This is all about our first Java program, we will see more details about class files in next lesson, let’s jump into our next lesson.

<<< Java TutorialJava and JVM Internals >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .