You are viewing a preview of this lesson. Sign in to start learning
Back to Java

Development Environment

Set up essential tools for Java development

Setting Up Your Java Development Environment

Master Java development setup with free flashcards and hands-on practice. This lesson covers installing the JDK, configuring your IDE, understanding environment variables, and troubleshooting common setup issuesβ€”essential foundations for every Java developer.

Welcome to Java Development Environment Setup πŸ’»

Before you can write your first line of Java code, you need a properly configured development environment. Think of this as setting up your workshop before building furnitureβ€”the right tools make all the difference. A Java development environment consists of three core components: the Java Development Kit (JDK), an Integrated Development Environment (IDE) or text editor, and properly configured environment variables. This lesson will guide you through each component, ensuring you're ready to start coding efficiently.

πŸ’‘ Did you know? Java's "write once, run anywhere" philosophy means that once you've set up your environment correctly, your code can run on any platform that supports Javaβ€”from servers to smartphones!

Understanding the Java Development Kit (JDK) β˜•

The JDK (Java Development Kit) is the foundation of Java development. It contains everything you need to compile and run Java programs.

What's Inside the JDK?

The JDK includes several critical components:

ComponentPurposeExample Usage
javacJava compiler - converts .java files to .class bytecodejavac HelloWorld.java
javaJava Runtime - executes compiled bytecodejava HelloWorld
jarArchive tool - packages classes into JAR filesjar cf app.jar *.class
javadocDocumentation generator - creates API docsjavadoc MyClass.java
JREJava Runtime Environment - runs Java applicationsEmbedded in JDK

JDK vs JRE vs JVM: The Hierarchy 🎯

Understanding the relationship between these three components is crucial:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              JDK                        β”‚
β”‚  (Java Development Kit)                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚         JRE                       β”‚  β”‚
β”‚  β”‚  (Java Runtime Environment)       β”‚  β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”‚
β”‚  β”‚  β”‚        JVM                  β”‚  β”‚  β”‚
β”‚  β”‚  β”‚  (Java Virtual Machine)     β”‚  β”‚  β”‚
β”‚  β”‚  β”‚  β€’ Bytecode interpreter     β”‚  β”‚  β”‚
β”‚  β”‚  β”‚  β€’ Just-In-Time compiler    β”‚  β”‚  β”‚
β”‚  β”‚  β”‚  β€’ Garbage collector        β”‚  β”‚  β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚
β”‚  β”‚  β€’ Class libraries               β”‚  β”‚
β”‚  β”‚  β€’ Configuration files           β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚  β€’ javac (compiler)                     β”‚
β”‚  β€’ jar, javadoc (tools)                 β”‚
β”‚  β€’ Debugger (jdb)                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key distinction: Developers need the JDK (for javac to compile code). End users only need the JRE (to run Java applications).

Choosing Your JDK Version πŸ”Ί

Java follows a predictable release cycle:

  • LTS (Long-Term Support) versions: Java 8, 11, 17, 21 (recommended for production)
  • Non-LTS versions: Released every 6 months with shorter support cycles

πŸ’‘ Best Practice: For learning, use the latest LTS version (Java 21 as of 2024). For enterprise projects, check your organization's standards.

Installing the JDK πŸ”§

Windows:

  1. Download from Oracle or Adoptium (OpenJDK)
  2. Run the installer (.exe file)
  3. Follow the wizard, noting the installation path (e.g., C:\Program Files\Java\jdk-21)
  4. The installer may configure PATH automatically

macOS:

## Using Homebrew (recommended)
brew install openjdk@21

## Or download .dmg from Oracle/Adoptium

Linux (Ubuntu/Debian):

## OpenJDK installation
sudo apt update
sudo apt install openjdk-21-jdk

## Verify installation
java -version
javac -version

⚠️ Common Mistake: Installing only the JRE instead of the JDK. Remember: JRE = run only, JDK = develop and run!

Configuring Environment Variables 🌍

Environment variables tell your operating system where to find Java executables. The two critical variables are JAVA_HOME and PATH.

What is JAVA_HOME?

JAVA_HOME is a pointer to your JDK installation directory. Many Java tools (Maven, Gradle, Tomcat) rely on this variable to locate the JDK.

Setting Up Environment Variables

Windows:

  1. Right-click "This PC" β†’ Properties β†’ Advanced system settings
  2. Click "Environment Variables"
  3. Under "System variables", click "New":
    • Variable name: JAVA_HOME
    • Variable value: C:\Program Files\Java\jdk-21 (your JDK path)
  4. Find the Path variable, click "Edit", then "New":
    • Add: %JAVA_HOME%\bin
  5. Click OK to save all changes
  6. Restart your terminal to apply changes

macOS/Linux (Bash):

Edit ~/.bashrc (Linux) or ~/.zshrc (macOS):

## Add these lines
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64  # Adjust path
export PATH=$JAVA_HOME/bin:$PATH

## Reload configuration
source ~/.bashrc  # or source ~/.zshrc

Verifying Your Configuration βœ…

Open a new terminal and run:

## Check Java runtime version
java -version

## Expected output:
openjdk version "21.0.1" 2023-10-17
OpenJDK Runtime Environment (build 21.0.1+12)
OpenJDK 64-Bit Server VM (build 21.0.1+12, mixed mode)

## Check compiler version
javac -version

## Expected output:
javac 21.0.1

## Verify JAVA_HOME (Windows)
echo %JAVA_HOME%

## Verify JAVA_HOME (macOS/Linux)
echo $JAVA_HOME

πŸ’‘ Pro Tip: If commands aren't recognized, ensure:

  1. Environment variables are saved
  2. Terminal was restarted after changes
  3. No typos in the JDK path

Choosing and Installing an IDE 🎨

An Integrated Development Environment (IDE) provides code editing, debugging, and project management in one application. While you can write Java in Notepad, an IDE dramatically boosts productivity.

IDEBest ForProsCons
IntelliJ IDEA Professional development Excellent code completion, refactoring, Spring support Community edition has limitations; resource-intensive
Eclipse Enterprise, plugin ecosystem Free, extensive plugins, good for large projects Steeper learning curve, less intuitive UI
Visual Studio Code Lightweight, multi-language Fast, customizable, great for small projects Requires extensions; less integrated than full IDEs
NetBeans Beginners, GUI development Easy to learn, good GUI builder, Apache-backed Smaller community than IntelliJ/Eclipse
  1. Download: Visit jetbrains.com/idea/download
  2. Choose Edition: Community (free) or Ultimate (paid, 30-day trial)
  3. Install: Run the installer, accepting defaults
  4. First Launch:
    • Select UI theme (Darcula for dark mode, Light for classic)
    • Skip plugin customization initially
    • IntelliJ will auto-detect your installed JDK

Creating Your First Project πŸš€

In IntelliJ IDEA:

  1. Click "New Project"
  2. Select "Java" from the left sidebar
  3. Project SDK: Select your installed JDK (e.g., "21")
    • If not listed, click "Add SDK" β†’ "JDK" β†’ navigate to your JDK folder
  4. Project name: HelloJava
  5. Location: Choose a folder (e.g., C:\Projects\HelloJava)
  6. Build system: None (for now; later you'll use Maven/Gradle)
  7. Click "Create"

Project Structure:

HelloJava/
β”œβ”€β”€ .idea/               # IntelliJ configuration
β”œβ”€β”€ src/                 # Source code folder
β”‚   └── Main.java       # Your Java files go here
└── HelloJava.iml       # Module file

Writing Your First Program πŸ’»

Right-click src β†’ New β†’ Java Class β†’ Name it Main:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Java World!");
    }
}

Running the Code:

  1. Method 1: Click the green ▢️ arrow next to public static void main
  2. Method 2: Right-click anywhere in the file β†’ Run 'Main.main()'
  3. Method 3: Press Shift + F10 (Windows/Linux) or Control + R (macOS)

Expected Output:

Hello, Java World!

Process finished with exit code 0

🧠 Memory Device: main method signature is "PSVM" β†’ Public Static Void Main. Every Java application starts here!

Understanding the Compilation Process πŸ”„

Java is a compiled language that produces platform-independent bytecode. Understanding this process helps troubleshoot issues.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         JAVA COMPILATION FLOW                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

  πŸ“ Write Code
       |
       ↓
  Main.java
  (Human-readable source)
       |
       ↓
  πŸ”¨ javac Compiler
       |
       ↓
  Main.class
  (Bytecode - platform independent)
       |
       ↓
  βš™οΈ JVM (java command)
       |
   β”Œβ”€β”€β”€β”΄β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   ↓       ↓        ↓         ↓
 Windows  macOS   Linux   Android
  (All run the same .class file!)

Manual Compilation (Understanding What IDEs Do)

Open terminal in your project folder:

## Compile source to bytecode
javac src/Main.java

## This creates src/Main.class

## Run the compiled bytecode
java -cp src Main

## Output: Hello, Java World!

Breakdown:

  • javac = Java Compiler
  • -cp src = ClassPath (where to find .class files)
  • Main = Name of class with main method (no .class extension!)

πŸ’‘ What IDEs Do: They automatically run javac when you click "Run", hiding this complexity. Understanding it helps when builds fail!

Common Environment Issues and Solutions πŸ”§

Problem 1: "javac is not recognized"

Symptom:

'javac' is not recognized as an internal or external command

Solutions:

  1. βœ… Verify JDK (not JRE) is installed: java -version should show "JDK"
  2. βœ… Check PATH includes %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (Unix)
  3. βœ… Restart terminal after setting variables
  4. βœ… On Windows, run as Administrator if PATH changes aren't persisting

Problem 2: Wrong Java Version

Symptom:

Error: LinkageError occurred while loading main class

Solution:

## Check all Java installations
where java      # Windows
which -a java   # macOS/Linux

## Ensure JAVA_HOME points to correct version
echo $JAVA_HOME

## Update PATH to prioritize correct JDK

Problem 3: IDE Can't Find JDK

In IntelliJ:

  1. File β†’ Project Structure (Ctrl+Alt+Shift+S)
  2. Project Settings β†’ Project
  3. SDK: Click "Edit" β†’ Add SDK β†’ JDK
  4. Navigate to your JDK folder (e.g., C:\Program Files\Java\jdk-21)
  5. Apply changes

Problem 4: Class Not Found Exception

Symptom:

Error: Could not find or load main class Main

Solutions:

  1. βœ… Ensure filename matches class name (Main.java for public class Main)
  2. βœ… Check classpath: java -cp . Main (if .class is in current directory)
  3. βœ… Verify package declarations match folder structure

⚠️ Common Mistake: Running java Main.class instead of java Main (no extension!)

Problem 5: Permission Denied (Linux/macOS)

Symptom:

bash: /usr/bin/java: Permission denied

Solution:

## Make Java executable
chmod +x /usr/bin/java

## Or reinstall with proper permissions
sudo apt install --reinstall openjdk-21-jdk

Advanced Configuration Tips πŸŽ“

Setting Up Multiple JDK Versions

Developers often need different Java versions for different projects:

Using SDKMAN (Linux/macOS):

## Install SDKMAN
curl -s "https://get.sdkman.io" | bash

## List available Java versions
sdk list java

## Install multiple versions
sdk install java 21.0.1-open
sdk install java 17.0.9-open
sdk install java 11.0.21-open

## Switch versions
sdk use java 17.0.9-open     # Current session
sdk default java 21.0.1-open # Set default

Manual (Windows):

  1. Install multiple JDKs in separate folders
  2. Change JAVA_HOME as needed:
    set JAVA_HOME=C:\Program Files\Java\jdk-17
    
  3. Or create batch scripts for quick switching

Build Tools: Maven and Gradle πŸ—οΈ

For real-world projects, you'll use build tools that automate compilation, dependency management, and testing:

Maven (XML-based):

<!-- pom.xml -->
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0</version>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
    </dependency>
  </dependencies>
</project>

Gradle (Groovy/Kotlin-based, more concise):

// build.gradle
plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'junit:junit:4.13.2'
}

IDEs automatically detect these files and configure projects accordingly. You'll learn these in depth later!

Practical Examples πŸ”¨

Example 1: Testing Your Environment

Create EnvironmentTest.java:

public class EnvironmentTest {
    public static void main(String[] args) {
        // Display system properties
        System.out.println("Java Version: " + 
            System.getProperty("java.version"));
        System.out.println("Java Home: " + 
            System.getProperty("java.home"));
        System.out.println("OS Name: " + 
            System.getProperty("os.name"));
        System.out.println("OS Version: " + 
            System.getProperty("os.version"));
        System.out.println("User Directory: " + 
            System.getProperty("user.dir"));
    }
}

Output Example:

Java Version: 21.0.1
Java Home: C:\Program Files\Java\jdk-21
OS Name: Windows 11
OS Version: 10.0
User Directory: C:\Projects\HelloJava

Why This Matters: This confirms your JDK version and helps diagnose environment-specific issues.

Example 2: Command-Line Arguments

public class ArgumentsDemo {
    public static void main(String[] args) {
        System.out.println("Number of arguments: " + args.length);
        
        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument " + i + ": " + args[i]);
        }
    }
}

Compile and run with arguments:

javac ArgumentsDemo.java
java ArgumentsDemo Hello World 123

Output:

Number of arguments: 3
Argument 0: Hello
Argument 1: World
Argument 2: 123

In IntelliJ: Run β†’ Edit Configurations β†’ Program arguments: Hello World 123

Example 3: Reading User Input

import java.util.Scanner;

public class InteractiveDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter your name: ");
        String name = scanner.nextLine();
        
        System.out.print("Enter your age: ");
        int age = scanner.nextInt();
        
        System.out.println("Hello, " + name + "!");
        System.out.println("You are " + age + " years old.");
        
        scanner.close();
    }
}

Running:

Enter your name: Alice
Enter your age: 25
Hello, Alice!
You are 25 years old.

Key Points:

  • Scanner reads input from various sources (keyboard, files)
  • Always close() scanners to free resources
  • nextLine() reads text, nextInt() reads integers

Example 4: Working with Packages

Proper project structure uses packages (folders organizing related classes):

src/
└── com/
    └── example/
        └── app/
            └── Main.java
package com.example.app;  // Must match folder structure!

public class Main {
    public static void main(String[] args) {
        System.out.println("Package: " + 
            Main.class.getPackage().getName());
    }
}

Compile and run:

## From project root
javac src/com/example/app/Main.java
java -cp src com.example.app.Main

Output:

Package: com.example.app

Why Packages? They prevent naming conflicts and organize large codebases. IDEs handle package structure automatically.

Common Mistakes to Avoid ⚠️

Mistake 1: Installing JRE Instead of JDK

Problem: You can run Java programs but not compile them.

Solution: Always install the JDK (Java Development Kit), which includes the JRE plus development tools.

Mistake 2: Not Restarting Terminal After Configuration

Problem: Environment variables don't take effect until terminal restarts.

Solution: After modifying JAVA_HOME or PATH, close and reopen all terminal windows.

Mistake 3: Mismatched Filename and Class Name

// File: MyProgram.java
public class MyApp {  // ❌ WRONG - names don't match!
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

Error:

MyProgram.java:1: error: class MyApp is public, should be declared in a file named MyApp.java

Solution: Filename MUST match the public class name exactly (case-sensitive):

// File: MyApp.java
public class MyApp {  // βœ… CORRECT
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

Mistake 4: Running .class File with Extension

java Main.class  # ❌ WRONG
java Main        # βœ… CORRECT

Java runtime expects the class name, not the filename!

Mistake 5: Ignoring IDE Warnings

⚠️ Warning: Unchecked operation
⚠️ Warning: Deprecated API usage

These warnings indicate potential issues. Don't ignore themβ€”fix them early to avoid bugs later.

Mistake 6: Not Using Version Control from Day One

Problem: Losing code or unable to track changes.

Solution: Initialize Git in your projects:

git init
git add .
git commit -m "Initial commit"

IDEs like IntelliJ have built-in Git support (VCS menu).

Key Takeaways 🎯

βœ… JDK is essential for Java developmentβ€”it includes the compiler (javac), runtime (java), and other tools

βœ… Environment variables (JAVA_HOME and PATH) tell your system where to find Java tools

βœ… IDEs boost productivity with code completion, debugging, and project managementβ€”IntelliJ IDEA Community Edition is excellent for beginners

βœ… Compilation process: .java source β†’ javac compiler β†’ .class bytecode β†’ java runtime β†’ execution

βœ… Testing your setup with simple programs ensures everything works before tackling complex projects

βœ… Common issues (javac not found, wrong version, class not found) are usually solved by checking environment variables and restarting terminals

βœ… Package structure organizes code and prevents naming conflictsβ€”IDEs handle this automatically

βœ… Build tools (Maven, Gradle) automate compilation and dependency management for real-world projects

πŸ“‹ Quick Reference Card

TaskCommand
Check Java versionjava -version
Check compiler versionjavac -version
Compile Java filejavac FileName.java
Run compiled classjava ClassName
View JAVA_HOME (Win)echo %JAVA_HOME%
View JAVA_HOME (Unix)echo $JAVA_HOME
Run with classpathjava -cp path ClassName
Create JAR filejar cf app.jar *.class

πŸ”‘ Essential Files

.javaHuman-readable source code
.classCompiled bytecode (platform-independent)
.jarJava Archive (packaged classes + resources)
pom.xmlMaven build configuration
build.gradleGradle build configuration

πŸ“š Further Study

  1. Oracle Java Documentation - https://docs.oracle.com/en/java/ - Official Java SE documentation, API references, and tutorials

  2. IntelliJ IDEA Documentation - https://www.jetbrains.com/idea/documentation/ - Comprehensive IDE guides, shortcuts, and productivity tips

  3. SDKMAN for Version Management - https://sdkman.io/ - Tool for managing multiple JDK versions on Unix-based systems

You're now ready to start writing Java code! The next lesson will dive into Java syntax fundamentals, where you'll learn about variables, data types, and operators. πŸš€