CS Engineering Gyan

Java Installation

Before you can start writing and running Java programs, you need to set up a proper development environment on your computer. This involves downloading the correct software, installing it, and configuring your system so that it recognizes Java commands from anywhere.

Many beginners feel unsure at this stage because installation steps can look technical at first. However, once you understand what each component does, the process becomes straightforward and only needs to be done once.

In this tutorial, you will learn what tools are required to run Java, how to install them correctly, how to configure environment variables, and how to write and execute your very first Java program.


What Do You Need to Run Java?

To write, compile, and run Java programs, your system needs a software package called the JDK (Java Development Kit). The JDK includes everything required for development, including the compiler that converts your code into bytecode and the runtime engine that executes it.

Component Purpose
JDK (Java Development Kit) Contains tools needed to write, compile, and debug Java programs.
Compiler (javac) Converts human-readable Java code into bytecode.
JVM (Java Virtual Machine) Executes the compiled bytecode on your system.
Text Editor or IDE Used to write and organize your Java source code.

Simply installing the JDK is enough to start coding, since it also includes the components needed to run your programs.


Downloading the JDK

The JDK is available as a free download and is offered by multiple providers, including Oracle and other open-source distributions such as OpenJDK. Both options work well for learning and development purposes.

Steps to Download

It is recommended for beginners to install a long-term support version, since these releases receive updates for a longer period and are widely supported by learning resources and tools.


Installing the JDK on Windows

Once the installer file is downloaded, installing Java on a Windows system follows a simple step-by-step wizard.

Installation Steps

After installation, Java files are placed inside a dedicated folder on your system, but your computer still needs to be told where to find these files whenever you type a Java command.


Setting Up Environment Variables

Environment variables allow your operating system to locate the Java installation whenever you run a command from the terminal or command prompt. Without this setup, your system may not recognize commands like java or javac.

Configuring JAVA_HOME

Setting the JAVA_HOME variable correctly ensures that development tools and build systems can automatically detect your Java installation.


Verifying the Installation

After installation and configuration, it is important to confirm that Java has been set up correctly. You can do this using simple commands in your terminal or command prompt.

Commands to Check Installation

java -version

javac -version

Expected Output

java version "21.0.1"

javac 21.0.1

If both commands display version details instead of an error message, your Java installation and environment variables have been configured successfully. The exact version number shown will depend on which release you installed.


Choosing an Editor or IDE

While Java programs can be written using any basic text editor, most developers prefer using a dedicated Integrated Development Environment (IDE) because it offers helpful features such as auto-completion, error detection, and built-in program execution.

Tool Best Suited For
Plain Text Editor Understanding manual compilation and execution as a beginner.
Lightweight Code Editor Writing Java code with useful extensions and a simple interface.
Full-Featured IDE Working on larger projects with advanced debugging and project management tools.

For absolute beginners, starting with a simple text editor and the command line helps build a strong understanding of how compilation and execution actually work before moving to a more advanced IDE.


Writing Your First Java Program

Once your environment is ready, you can create your first Java file. Java source files always use the .java extension, and the file name must match the name of the public class inside it.

Example

public class FirstProgram {

    public static void main(String[] args) {

        System.out.println("Java installation successful!");

    }

}

Save this file with the exact name FirstProgram.java, since it must match the public class name written inside the code.


Compiling and Running the Program

Java programs go through two separate steps before you see any output: compilation and execution. Each step uses a different command in the terminal.

Step 1: Compile the Program

javac FirstProgram.java

This command converts your source code into a bytecode file with a .class extension. If there are no errors in your code, this step will complete without displaying any message.

Step 2: Run the Program

java FirstProgram

Output

Java installation successful!

Notice that the run command does not include the file extension. This is because the java command executes the compiled class file, not the original source file.


Common Installation Errors

Error Possible Reason
'java' is not recognized as a command The Path environment variable was not configured correctly.
Class name does not match file name The saved file name does not match the public class name inside the code.
Could not find or load main class The program was run from a different folder than where the compiled file exists.
Compilation succeeds but nothing runs The file extension was mistakenly included while running the program.

Best Practices During Installation


Frequently Asked Interview Questions

  1. What is the difference between JDK, JRE, and JVM during installation?
  2. Why is it necessary to set the JAVA_HOME environment variable?
  3. What command is used to compile a Java program?
  4. What command is used to run a compiled Java program?
  5. Why must the file name match the public class name?
  6. How can you verify that Java has been installed correctly?
  7. What happens if the Path variable is not configured properly?
  8. What is the purpose of a .class file generated after compilation?

Summary

Setting up Java correctly is the first practical step toward becoming a Java developer. This involves downloading the JDK, installing it on your system, configuring environment variables, and verifying that everything works using simple terminal commands.

Once your environment is ready, writing, compiling, and running a Java program becomes a quick and repeatable process. With this foundation in place, you are now ready to explore how a Java program is structured and how each part of the code contributes to its execution.


← Previous: Introduction to Java Next: Java Program Structure →

Home Visit Our YouTube Channel