Simple Java Example

This tutorial will explain how to download and install Java on your computer. We will also show you how simple it is to write a working java program.

Download and Install Java

Java can be freely downloaded from Oracle’s website. You need  the Java JDK to be able to write your own Java programs. JDK stands for Java Development Kit and it includes everything you will need to accomplish small or big projects.

If you need to install Java on Ubuntu Linux or LinuxMint please check the Java Ubuntu installation guide

In this tutorial I will show you how to install Java version 8. First go to

Scroll down to Java SE Development Kit.

Now click on the download link next to your operation system to download the installation file. Please note, you have to accept the license agreement to be able to do this. After downloading, start the installation and go trough the setup wizard. Note the path where you are installing Java.

The setup wizard will install JDK and JRE on your computer. JDK is the one you need to write your own java programs and JRE (Java Runtime Environment) is the interpreter program which executes java programs on your computer. You will need both for this example to work.

After you are done with installation open a command prompt on your PC (or terminal on UNIX-based systems like Mac and Linux). Type in

java -version

If the command returns a build number (see the screenshot below) then everything went well.

java version command result

The result of java -version command on Windows

Your First Java Program

Now you are ready to write your first java program. Open your favorite text editor program. Please don’t use programs for rich text editing like Microsoft Word or similar. Use Notepad (or Notepad++, UltraEdit, gEdit etc.).

Write following code into a new file and save it as Note the file extension must be .java

public class MyFirstJavaProgram {
	public static void main(String[] args) {
		System.out.println("Hello, this is my first java program");

We will use javac (the Java compiler) to build this program. Now open the command prompt again and type in:

C:\>d:\Development\Java\jdk8\bin\javac.exe d:\Development\\MyFir

The first command is the path to javac.exe (the java compiler), which in my case is D:\Development\Java\jdk8\bin\javac.exe and the second is the path to your .java file

One important thing you must know – the file name and java class name must be the same. In this tutorial it is MyFirstJavaProgram

Now you are ready to execute your first program. Still in command prompt browse to your working folder (in my case it is D:\Development\ and type in:

java MyFirstJavaProgram

You should see following result

The result of executing our first java program

The result of executing our first java program

Explaining the Code

Now lets go back to the source code of our example and take a closer look.

public class MyFirstJavaProgram

In Java you need a public class to make a program executable. Everything in Java is build upon classes and interfaces. You will learn more about this in our next tutorials.

public static void main(String[] args) {

This is the main method of our program. It is automatically called by Java when you execute the program. It must have exactly the structure shown above in every java program you write

System.out.println("Hello, this is my first java program");

System.out.println displays a text in the console and appends a new line to it.


In our next tutorial Java Eclipse Tutorial I will show you how to use the Eclipse IDE to simplify code writing, building and execution of Java programs.

5 1 vote
Article Rating
Inline Feedbacks
View all comments