Navigation: Main Tutorial Index | Previous: Introduction | Next: Java Basics
Examples: HelloWorld.java
Before writing Java programs, we need to install the Java Development Kit (JDK) on our computer.
In this tutorial, we will learn how to install Java 8, configure the environment, check Java versions, and execute our first Java program.
JDK (Java Development Kit) is a software package that provides all the necessary tools to develop Java applications.
It includes:
- Java Compiler (
javac) - Java Launcher (
java) - Java Runtime Components
- Debugging Tools
- Documentation Tools
Important: If you want to develop Java applications, you should install the JDK rather than only a runtime environment.
Follow these steps to download Java 8.
Step 1: Visit the official website
Download JDK 8 from Oracle:
https://www.oracle.com/java/technologies/downloads/#java8
Alternatively, you can use Eclipse Temurin OpenJDK:
https://adoptium.net/temurin/releases/?version=8
Step 2: Select your operating system
Choose the appropriate installer according to your operating system:
- Windows
- macOS
- Linux
Step 3: Install JDK 8
Run the downloaded installer and follow the installation instructions.
After installation, remember the location where JDK is installed.
Common installation locations:
Windows:
C:\Program Files\Java\jdk1.8.0_xxx
macOS:
/Library/Java/JavaVirtualMachines/
Linux:
/usr/lib/jvm/
The exact directory depends on the JDK distribution and version.
After installing Java, open your terminal or command prompt.
Execute the following command:
java -versionExpected output (version details may differ):
java version "1.8.0_XXX"
Java(TM) SE Runtime Environment
Java HotSpot(TM) 64-Bit Server VM
Now check the Java compiler version:
javac -versionExpected output:
javac 1.8.0_XXX
If both commands work, Java is installed and the Java compiler is available.
Remember:
javais used to launch Java applications.javacis used to compile Java source code.
An environment variable is a system-level setting that stores information about the environment in which programs run.
In Java development, two important environment variables are:
1. JAVA_HOME
JAVA_HOME stores the installation path of the JDK.
Example:
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_xxx
2. PATH
PATH tells the operating system where to find executable commands.
We add the JDK bin directory to PATH so that commands such as java and javac can be executed from any terminal directory.
Example:
%JAVA_HOME%\bin
Follow these steps:
- Install JDK 8.
- Open Windows Search.
- Search for Environment Variables.
- Select Edit the system environment variables.
- Click the Environment Variables button.
- Under System Variables, click New.
- Enter the variable name:
JAVA_HOME
- Enter the JDK installation path as its value.
Example:
C:\Program Files\Java\jdk1.8.0_xxx
- Find the
Pathvariable under System Variables. - Click Edit and add:
%JAVA_HOME%\bin
- Save all changes.
Close and reopen the terminal.
Now execute:
java -version
javac -versionIf you are using macOS, follow these steps.
Step 1: Install JDK 8
Download the macOS installer from Oracle or Eclipse Temurin.
Step 2: Check installed Java versions
Open Terminal and execute:
/usr/libexec/java_home -VThis command displays the installed JDK versions.
Step 3: Set JAVA_HOME
For Java 8, execute:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)Step 4: Add Java to PATH
Execute:
export PATH="$JAVA_HOME/bin:$PATH"Step 5: Verify installation
java -version
javac -version
echo $JAVA_HOMETo make these settings permanent for the Zsh shell, add the following lines to your ~/.zshrc file:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export PATH="$JAVA_HOME/bin:$PATH"Save the file and reload it:
source ~/.zshrcAn IDE (Integrated Development Environment) is software that helps developers write, compile, run, and debug programs.
Popular Java IDEs include:
1. IntelliJ IDEA
A popular Java IDE with intelligent code completion, debugging, and project management features.
Website: https://www.jetbrains.com/idea/
2. Eclipse IDE
An open-source IDE widely used for Java application development.
Website: https://eclipseide.org/
3. Visual Studio Code
A lightweight code editor that supports Java development through extensions.
Website: https://code.visualstudio.com/
4. NetBeans
An IDE that provides tools for Java development and project management.
Website: https://netbeans.apache.org/
For beginners, IntelliJ IDEA Community Edition or Eclipse IDE is a good starting point.
Now that Java is installed, let's create our first program.
Create a file named:
HelloWorld.java
Write the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Welcome to Java Programming!");
}
}Output:
Welcome to Java Programming!
You can compile and execute Java programs without using an IDE.
Step 1: Open Terminal or Command Prompt
Navigate to the directory where your Java file is saved.
Step 2: Compile the Java program
javac HelloWorld.javaAfter successful compilation, Java generates:
HelloWorld.class
This file contains Java bytecode.
Step 3: Execute the Java program
java HelloWorldOutput:
Welcome to Java Programming!
Important: While executing the program, do not include the .java or .class extension after the class name.
Correct:
java HelloWorldIncorrect:
java HelloWorld.javaThe above incorrect command is not the traditional Java 8 compile-and-run workflow covered in this tutorial.
Reason: Java is not installed correctly, or its bin directory is missing from PATH.
Solution:
- Check whether JDK is installed.
- Configure JAVA_HOME.
- Add the JDK
bindirectory to PATH. - Restart the terminal.
Reason: The JDK compiler is unavailable through the configured PATH.
Solution:
Verify that the JDK is installed and add its bin directory to PATH.
Reason: Java cannot locate the class you want to execute.
Solution:
- Check the class name.
- Ensure compilation was successful.
- Verify that you are in the correct directory.
- Check the classpath if necessary.
Reason: The Java runtime is older than the version used to compile the class.
Solution: Use a compatible or newer Java runtime, or compile the source code for the target Java version.
| Command | Purpose |
|---|---|
java -version |
Check Java version |
javac -version |
Check compiler version |
javac HelloWorld.java |
Compile Java source code |
java HelloWorld |
Run Java application |
echo %JAVA_HOME% |
Display JAVA_HOME on Windows |
echo $JAVA_HOME |
Display JAVA_HOME on macOS/Linux |
where java |
Locate Java executable on Windows |
which java |
Locate Java executable on macOS/Linux |
javadoc |
Generate Java documentation |
jar |
Create and manage JAR files |
Try answering these questions to test your understanding.
- What is JDK? Answer: JDK is the Java Development Kit, a development package that includes the compiler, launcher, libraries, tools, and a JRE needed to build and run Java applications.
- Why do we need to install JDK before developing Java applications? Answer: The JDK provides the compiler and tools that convert Java source code into bytecode and support application development.
- What is the difference between
javaandjavac? Answer:javaccompiles.javasource files into.classbytecode, whilejavastarts the JVM to run a compiled Java application or class. - What is JAVA_HOME? Answer: JAVA_HOME is an environment variable that identifies the JDK installation directory.
- Why do we configure the PATH variable?
Answer: PATH lets the operating system find commands such as
java,javac, andjavadocwithout requiring their full installation paths. - What is an IDE? Answer: An IDE is an integrated development environment that combines a code editor, compiler or build integration, debugger, and project tools.
- What is the extension of a Java source file?
Answer: A Java source file uses the
.javaextension. - What is the extension of a compiled Java bytecode file?
Answer: A compiled Java class file uses the
.classextension. - Which command is used to check the Java version?
Answer: Use
java -versionto check the runtime version andjavac -versionto check the compiler version. - How do you compile and execute a Java program using the terminal?
Answer: Use
javac Main.javato compile it, thenjava Mainto execute theMainclass.
- JDK is required to develop Java applications.
- Java source files use the
.javaextension. - Java compiler converts source code into bytecode.
- Compiled Java files use the
.classextension. javaccompiles Java programs.javalaunches Java applications.- JAVA_HOME stores the JDK installation path.
- PATH allows the operating system to locate Java commands.
- IDEs make Java development easier.
Topic 3: Java Basics – Syntax, Variables, Data Types, and Operators
In the next tutorial, we will learn how to write Java statements, declare variables, understand primitive and non-primitive data types, and perform operations using Java operators.
Keep Learning, Keep Practicing, and Keep Coding!