Installing Java and Setting Up Environment
# CHAPTER 2
Installing Java and Setting Up Environment
1. Introduction
Before you can write a single line of Java, you need the right tools installed on your computer. Think of it like cooking: you need a kitchen (JDK), utensils (IDE), and a recipe book (documentation) before you can make your first dish. In this chapter, we'll install everything from scratch and verify it works.2. Learning Objectives
By the end of this chapter, you will be able to:- Download and install the Java Development Kit (JDK).
-
Configure the
JAVA_HOMEenvironment variable.
- Install and configure IntelliJ IDEA or Eclipse.
- Compile and run a Java program from the command line.
- Understand the compilation and execution process.
3. Step 1: Downloading the JDK
Visit the official Oracle website or use the open-source OpenJDK:- Oracle JDK: https://www.oracle.com/java/technologies/downloads/
- OpenJDK (Adoptium/Temurin): https://adoptium.net/
Download JDK 21 LTS (Long Term Support) for your operating system.
4. Step 2: Installing the JDK
Windows:
-
1.
Run the
.msiinstaller.
- 2. Accept the license and click "Next" through the wizard.
-
3.
Note the installation path (default:
C:\Program Files\Java\jdk-21).
macOS:
-
1.
Run the
.dmginstaller.
- 2. Follow the on-screen instructions.
Linux (Ubuntu/Debian):
5. Step 3: Setting JAVAHOME
Why? Many Java tools (Maven, Gradle, Tomcat) look for the JAVAHOME variable to find the JDK.
Windows:
- 1. Open System Properties → Advanced → Environment Variables.
- 2. Click "New" under System Variables.
-
3.
Variable name:
JAVAHOME
-
4.
Variable value:
C:\Program Files\Java\jdk-21
-
5.
Add
%JAVAHOME%\binto thePathvariable.
macOS/Linux:
6. Step 4: Verifying Installation
Open your terminal or command prompt and type:Expected Output:
Also verify the compiler:
Expected Output:
If both commands work, Java is installed correctly!
7. Step 5: Installing an IDE
Option A: IntelliJ IDEA (Recommended)
- Download from: https://www.jetbrains.com/idea/
- Community Edition is free and perfect for learning.
- Features: Intelligent code completion, refactoring, debugger, and built-in terminal.
Option B: Eclipse
- Download from: https://www.eclipse.org/downloads/
- Free and open-source.
- Well-suited for enterprise Java development.
Option C: VS Code
- Install the "Extension Pack for Java" by Microsoft.
- Lightweight and fast for small projects.
8. Step 6: Compiling and Running from Command Line
The Java compilation process has two stages:
Step 1: Create a file called Hello.java:
Step 2: Compile with javac:
This creates a file called Hello.class (the bytecode).
Step 3: Run with java:
Output:
Important: The filename must match the public class name. Hello.java must contain public class Hello.
9. Understanding the Compilation Process
10. Mini Project: Personal Info Printer
11. Common Mistakes
-
Not matching filename to class name:
MyApp.javamust containpublic class MyApp. Mismatches cause compilation errors.
-
Forgetting to set PATH: If
java -versionsays "command not found," thePATHvariable is not configured correctly.
-
Using a text editor without saving as
.java: Saving as.txtwill fail compilation.
12. Best Practices
- Always use the latest LTS version of Java (17 or 21).
- Use IntelliJ IDEA for the best developer experience.
- Learn to compile from the command line first before relying on an IDE — it builds fundamental understanding.
13. Exercises
-
1.
Install JDK 21 on your machine and verify it with
java -version.
- 2. Write a program that prints your full name, city, and favorite programming language.
-
3.
Compile and run it from the command line using
javacandjava.
14. MCQ Quiz with Answers
Which tool is used to compile Java source code?
What file extension does a Java source file have?
What file is generated after compilation?
What environment variable points to the JDK installation?
Which IDE is developed by JetBrains for Java?
What is the correct command to run a compiled Java class called Hello?
The .class file contains what type of code?
Which command verifies your Java installation?
What happens if your filename doesn't match the public class name?
What does the JIT compiler do?
15. Interview Questions
- Q: Explain the Java compilation and execution process step by step.
-
Q: What is the difference between
javacandjavacommands?
- Q: Why does the filename in Java need to match the public class name?
- Q: What is Just-In-Time (JIT) compilation?
16. FAQs
Q: Do I need to pay for Java? A: No. OpenJDK is completely free and open-source. Oracle JDK is free for development but requires a license for commercial production use in some versions.Q: Can I use VS Code for Java? A: Yes, with the "Extension Pack for Java" installed, VS Code becomes a capable Java development environment.
17. Summary
Setting up your Java development environment involves installing the JDK, configuringJAVA_HOME, and choosing an IDE. The Java compilation process is two-step: javac compiles .java files into .class bytecode, and java runs that bytecode on the JVM. With your environment ready, you're now prepared to write real Java code.
18. Next Chapter Recommendation
Your environment is set up and running. In Chapter 3: Java Syntax and First Program, we'll dive deep into the anatomy of a Java program — understanding every keyword, themain method, comments, and building your first real calculator.