Before writing and running Java programs, you need to install a Java Development Kit (JDK). The JDK provides the compiler, runtime tools, and other utilities required to develop Java applications.
JDK stands for Java Development Kit. It is a collection of tools used to develop, compile, and run Java programs.
You need a JDK to compile Java source code using the javac command.
JDK, JRE, and JVM are related Java terms, but they have different meanings.
| Term | Full Form | Purpose |
|---|---|---|
| JDK | Java Development Kit | Provides tools for developing Java programs. |
| JRE | Java Runtime Environment | Traditional term for the components needed to run Java applications. |
| JVM | Java Virtual Machine | Executes Java bytecode. |
Before installing Java, check that your computer supports the JDK distribution you plan to use.
Exact requirements depend on the JDK version and distribution you choose.
Download a JDK from an official vendor or an established OpenJDK distribution.
Basic steps:
Follow these steps to install a JDK on Windows using an installer package.
C:\Program Files\Java\jdk-21
This is an example path only. The actual folder name depends on the JDK version and vendor.
JAVA_HOME is an environment variable commonly used by Java development tools to identify the JDK installation directory.
C:\Program Files\Java\jdk-21
Steps on Windows:
Use your actual installation path. Do not include
the bin folder in the JAVA_HOME value.
The PATH environment variable allows you to run Java commands from Command Prompt without typing the complete path to the executable.
Add the JDK's bin folder to PATH.
%JAVA_HOME%\bin
Steps:
%JAVA_HOME%\bin.After installing Java, verify that the Java executable is available.
java -version
This command displays information about the Java runtime being used.
javac -version
This command displays the version of the Java compiler.
java version "21..."
javac 21...
The exact output depends on your installed JDK. Both commands should work when the JDK is installed correctly and its tools are available through PATH.
On Linux, Java can often be installed through the distribution's package manager or from an official JDK archive.
sudo apt update
sudo apt install openjdk-21-jdk
The package must be available in your configured repositories. If it is unavailable, use a supported JDK package or installation method for your distribution.
java -version
javac -version
On macOS, you can install a compatible JDK using a vendor-provided installer or an appropriate package manager.
java -version
javac -version
Ensure you choose a JDK build that supports your Mac's processor architecture.
Beginners may encounter errors while installing Java or configuring environment variables.
| Problem | Possible Solution |
|---|---|
| java is not recognized | Check PATH and open a new terminal. |
| javac is not recognized | Install a JDK and check its bin directory. |
| Wrong Java version | Check PATH order and the active Java installation. |
| Installation fails | Verify system compatibility and installer requirements. |
| JAVA_HOME not working | Set JAVA_HOME to the JDK root directory. |
java -version
javac -version
where java
where javac
The where commands are for Windows.
On Linux or macOS, use which java and
which javac.
Once the JDK is installed, create a Java source file and compile it from your terminal.
public class Main {
public static void main(String[] args) {
System.out.println("Java is installed successfully!");
}
}
javac Main.java
java Main
Run these commands in the folder containing
Main.java. The filename must match
the public class name.
1. Which Java component is needed to compile Java source code?
2. Which command displays the Java compiler version?
3. Which command runs a compiled Main class?