- How to write, compile and run a hello world Java program for beginners
- 1. Download and install Java Development Kit
- 2. Set up environment variables
- 3. Code a Java hello world program
- 4. Compile your first Java program
- 5. Run your first Java program
- 6. What we have learnt so far
- Related Java Hello World Tutorials:
- About the Author:
- How to Create and Run Your First Java Program
- 2. Run Your First Java Program
- 2.1 Prerequisites
- 2.2 Download
- 2.3 Setup
- 2.4 First Program
- 2.4.1 Checking the running of the program
- 2.4.2 Command Line Arguments
- 2.4.3 Java Class with Constructor
- 2.4.4 Java Class with setter and getter
- 2.5 Error Handling
- 2.6 Input
- 2.7 Expressions
- 2.8 Blocks
- 2.9 Comments
- 3. Download the Source Code
How to write, compile and run a hello world Java program for beginners
If you are new to Java programming and wish to learn it right now by doing some hands-on practice, you have come to the right place. This tutorial will help you writing your first Java program, typically a “hello world” one — your first step of the adventure into Java programming world. Throughout this tutorial, you will learn fundamental concepts and steps which are necessary for every Java fresher.
To start, all you need is a fresh computer without any Java software installed, a text-based editor and a good internet connection.
NOTES: This beginner tutorial is targeted for Windows environment.
1. Download and install Java Development Kit
In order to write and run a Java program, you need to install a software program called Java SE Development Kit (or JDK for short, and SE means Standard Edition). Basically, a JDK contains:
-
- JRE(Java Runtime Environment): is the core of the Java platform that enables running Java programs on your computer. The JRE includes JVM(Java Virtual Machine) that runs Java programs by translating from bytecode to platform-dependent code and executes them (Java programs are compiled into an intermediate form called bytecode), and other core libraries such as collections, File I/O, networking, etc.
- Tools and libraries that support Java development.
-
- javac.exe : is Java compiler that translates programs written in Java code into bytecode form.
- java.exe : is the Java Virtual Machine launcher that executes bytecode.
Check the option “Accept License Agreement”, and choose an appropriate version for your computer from the list. Here we choose the version for Windows x64:
After downloading the program, run it to install the JDK on your computer (just following the steps, leave the defaults and click Next, Next…):
You would see the JDK is installed in the following directory, for example: C:\Program Files\Java\jdk1.7.0_21. The following screenshot describes the JDK’s directory structure:
Now let’s test if Java runtime is installed correctly. Open a command prompt window and type:
java -version
You would see the following result:
That shows version of the JRE, e.g. “1.7.0_21” — Congratulations! Your computer is now capable of running Java programs.
Now try to type the following command:
javac -version
You would see the following error:
That’s because Windows could not find the javac program, so we need to set some environment variables which tell the location of javac.exe .
2. Set up environment variables
Now we’re going to set environment variables so that the javac.exe program can be accessed anywhere from command line. On Windows 7, go to My Computer and click System Properties:
Then click Advanced system settings:
The System Properties dialog appears, select Advanced tab and click Environment Variables. :
The Environment Variable dialog appears, click on the New… button under the System variables section.
That opens up the New System Variable dialog. Type the following information:
The field Variable name must be JAVA_HOME , and the field Variable value must point to JDK’s installation directory on your computer. Here it is set to c:\Program Files\Java\jdk1.7.0_21. Click OK to close this dialog.
Now back to the Environment Variables dialog, look for a variable called Path under the System Variables list, and click Edit…:
In the Edit System Variable dialog, append the following to the end of the field Variable value:
;%JAVA_HOME%\bin
Note that there is a semicolon at the beginning to separate this value from other ones. Click OK three times to close all the dialogs.
Now we have to quit the current command prompt and open a new one to see the changes takes effect. Type the following command again in the re-opened command prompt window:
javac -version
You would see the following output:
Congratulations! You have completed the setup for essential Java development environment on your computer. It’s now ready to write your first Java program.
3. Code a Java hello world program
Save the file as HelloWorld.java (note that the extension is .java ) under a directory, let’s say, C:\Java.
Don’t worry if you don’t understand everything in this simple Java code. The following picture explains it nicely:
Every Java program starts from the main() method. This program simply prints “Hello world” to screen.
4. Compile your first Java program
Now let’s compile our first program in the HelloWorld.java file using javac tool. Type the following command to change the current directory to the one where the source file is stored:
And type the following command:
javac HelloWorld.java
That invokes the Java compiler to compile code in the HelloWorld.java file into bytecode. Note that the file name ends with .java extension. You would see the following output:
If everything is fine (e.g. no error), the Java compiler quits silently, no fuss. After compiling, it generates the HelloWorld.class file which is bytecode form of the HelloWorld.java file. Now try to type dir in the command line, we’ll see the .class file:
So remember a Java program will be compiled into bytecode form (.class file).
5. Run your first Java program
java HelloWorld
That invokes the Java Virtual Machine to run the program called HelloWorld (note that there is no .java or .class extension). You would see the following output:
It just prints out “Hello world!” to the screen and quits. Congratulations! You have successfully run your first Java program!
6. What we have learnt so far
-
- JDK is the Java SE Development Kit that contains tools and libraries for Java development.
- JRE is the Java Runtime Environment that enables running Java programs on your computer.
- JVM is the Java Virtual Machine that actually executes Java programs. With JVM, programs written in Java can run on multi-platforms (thus Java is called cross-platform language).
- How to install JDK and configure environment variables.
- javac is the Java compiler. It translates Java source code into bytecode.
- java is the JVM launcher which we use to run our program.
- Every Java program starts from the main() method.
- When compiling, the compiler generates a .class file from a .java file.
You can also watch the video version of this tutorial:
Next, I recommend you to read this article: Understand Classes and Objects in Java
Related Java Hello World Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.
How to Create and Run Your First Java Program
This is an in-depth article on how to create and run your first java program. Java compiler is used to compile java code. Java class is the output of the compilation. To execute the program you need java runtime virtual machine.
2. Run Your First Java Program
2.1 Prerequisites
Java 7 or 8 is required on the linux, windows or mac operating system.
2.2 Download
You can download Java 7 from Oracle site. On the other hand, You can use Java 8. Java 8 can be downloaded from the Oracle web site .
2.3 Setup
You can set the environment variables for JAVA_HOME and PATH. They can be set as shown below:
Environmental Variables setup
JAVA_HOME=”/desktop/jdk1.8.0_73″ export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH export PATH
2.4 First Program
2.4.1 Checking the running of the program
You need to create a java class «FirstJavaProgram» . The class needs to have static method «main» which takes string array as the arguments. The first program in java is shown below. It prints “Checking First java program”.
public class FirstJavaProgram < public static void main(String[] args) < System.out.println("checking First java "); >>
The command below executes the above code snippet:
javac FirstJavaProgram.java java FirstJavaProgram
The output of the executed command is shown below.
2.4.2 Command Line Arguments
You can pass the command line arguments to the java program. The static method «main» has the string array which will have the command line arguments. The code is modified to handle command line arguments.
public class JavaProgramArguments < public static void main(String[] args) < System.out.println("checking the arguments "); int i=0; for(String arg: args) < i++; System.out.println("argument "+i+" value "+ arg); >> >
The command below executes the above code snippet: Run Command
javac JavaProgramArguments.java java JavaProgramArguments 11 22 33
The output of the executed command is shown below.
2.4.3 Java Class with Constructor
The java program can have a constructor. The «Program» class can have a public method «output» . In the «main» method, program is instantiated and the method on the object is invoked. The code is shown below. Program Class
public class Program < public Program() < >public void output(String value) < System.out.println(value); >public static void main(String[] args) < Program program = new Program(); program.output("checking class with constructor"); >>
The command below executes the above code snippet: Run Command
javac Program.java java Program
The output of the executed command is shown below.
2.4.4 Java Class with setter and getter
You can create a java class Person which has id as a private member. It can have a default constructor with setter and getter methods for «id» . The code for the java class is shown below. Person Class
public class Person < private String id; public Person() < >public String getId() < return id; >public void setId(String id) < this.id = id; >public static void main(String[] args) < Person person = new Person(); person.setId("34567"); System.out.println("Person's id" + person.getId()); >>
The command below executes the above code snippet: Run Command
javac Person.java java Person
The output of the executed command is shown below.
2.5 Error Handling
Errors can happen during compilation or runtime. Compile time errors happen during the compilation of the program. Run-time errors can happen when you run the program. Logical errors are related to the code and the wrong results.
2.6 Input
You can use the «java.util.Scanner» class to input values to the java program. The «java.util.Scanner» has methods to handle long, float, double and String types. The methods are «nextLong()» , «nextFloat()» , «nextDouble()» and «next()» . The code below shows how «java.util.Scanner» class can be used. Java Input
import java.util.Scanner; public class JavaInput < public static void main(String[] args) < Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); double number = scanner.nextDouble(); System.out.println("The number inputted is " + number); >>
The command below executes the above code snippet: Run Command
javac JavaInput.java java JavaInput
The output of the executed command is shown below.
2.7 Expressions
You can use expressions in the code. Expressions are variables, operators, literals and method calls. They evaluate to one value. The sample code is shown below Expressions
public class JavaExpressions < public static void main(String[] args) < double number; number = 65.0; System.out.println(number); double newNumber; newNumber = 45.0; if(newNumber < number) < System.out.println("45 is less than 65"); >> >
The command below executes the above code snippet: Run Command
javac JavaExpressions.java java JavaExpressions
The output of the executed command is shown below.
2.8 Blocks
A block of code consists of one or more statements. These statements are enclosed in curly braces < >in if condition, for, do-while and while loops.
2.9 Comments
In the program, you can add comments at the class level and the methods. You can use block comments or single line comments. The suggested best practices for commenting code can be accessed at the oracle website. start command
/** Person class */ public class Person < // id property private String id; /** * default constructor */ public Person() < >/** * getter method for Id */ public String getId() < return id; >/** * setter method for Id */ public void setId(String id) < this.id = id; >/** * static method main */ public static void main(String[] args) < Person person = new Person(); person.setId("34567"); System.out.println("Person's id is " + person.getId()); >>
3. Download the Source Code
Download
You can download the full source code of this example here: How to Create and Run Your First Java Program