Skip to main content
  1. Resources/
  2. Study Materials/
  3. Information & Communication Technology Engineering/
  4. ICT Semester 4/
  5. Java Programming (4343203)/

7 mins· ·
Milav Dabgar
Author
Milav Dabgar
Experienced lecturer in the electrical and electronic manufacturing industry. Skilled in Embedded Systems, Image Processing, Data Science, MATLAB, Python, STM32. Strong education professional with a Master’s degree in Communication Systems Engineering from L.D. College of Engineering - Ahmedabad.
Lecture 02 - Java Environment Setup & First Program

Java Programming

Lecture 02: Environment Setup & First Program

Course: 4343203 - Java Programming

GTU Semester 4 | Unit 1


Learning Objectives:

  • Install and configure Java Development Kit (JDK)
  • Understand JDK, JRE, and JVM relationships
  • Set up development environment and IDE
  • Write, compile, and execute first Java program
  • Understand Java compilation process

Java Platform Components

Java Platform Architecture

JDK (Java Development Kit)

  • Complete development environment
  • Includes JRE + Development tools
  • javac (compiler), java (runtime), javadoc

JRE (Java Runtime Environment)

  • Runtime environment only
  • Includes JVM + Core libraries
  • For running Java applications

Relationship: JDK ⊃ JRE ⊃ JVM

For development, you need JDK. For deployment, JRE is sufficient.

JDK Installation Process

Step 1: Download JDK

  • Oracle JDK: oracle.com/java/technologies/downloads/
  • OpenJDK: adoptopenjdk.net or jdk.java.net
  • Recommended: JDK 11 or 17 (LTS versions)

Step 2: Choose Platform

  • Windows x64 (.exe installer)
  • macOS x64 (.dmg installer)
  • Linux x64 (.tar.gz or .deb/.rpm)

Step 3: Install JDK

  • Run installer with administrator privileges
  • Choose installation directory (note the path)
  • Complete installation process

Setting Environment Variables

Windows Setup:

1. Set JAVA_HOME

  • Right-click "This PC" → Properties
  • Advanced System Settings
  • Environment Variables
  • New System Variable:
Variable: JAVA_HOME
Value: C:\Program Files\Java\jdk-17

2. Update PATH

  • Edit PATH variable
  • Add: %JAVA_HOME%\bin

Linux/macOS Setup:

1. Edit Profile (.bashrc/.zshrc)

export JAVA_HOME=/usr/lib/jvm/java-17
export PATH=$JAVA_HOME/bin:$PATH

2. Reload Profile

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

Why Environment Variables?

Allows running Java commands from any directory and helps IDEs locate Java installation automatically.

Installation Verification

Command Line Verification:

# Check Java Runtime
java -version

# Check Java Compiler
javac -version

# Check JAVA_HOME
echo $JAVA_HOME # Linux/macOS
echo %JAVA_HOME% # Windows

Expected Output:

$ java -version
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12)
OpenJDK 64-Bit Server VM (build 17.0.1+12)

$ javac -version
javac 17.0.1

Common Issues:

  • 'java' is not recognized: PATH not set correctly
  • Different versions: Multiple Java installations
  • Permission denied: Administrator rights needed

IDE Selection & Setup

Popular Java IDEs:

  • IntelliJ IDEA (Community/Ultimate)
  • Eclipse IDE (Free)
  • NetBeans (Free)
  • VS Code + Java Extension Pack

IDE Features:

  • Syntax highlighting & auto-completion
  • Debugging support
  • Project management
  • Version control integration
  • Refactoring tools

VS Code Setup (Recommended for beginners):

  1. Download and install VS Code
  2. Install "Extension Pack for Java"
  3. Reload VS Code
  4. Verify Java detection in status bar

IntelliJ IDEA Setup:

  1. Download Community Edition
  2. Install with default settings
  3. Configure SDK path to JDK
  4. Create new Java project

Your First Java Program

HelloWorld.java


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

Code Explanation:

  • public class HelloWorld: Class declaration
  • public static void main: Entry point method
  • String[] args: Command line arguments
  • System.out.println: Print statement
  • Curly braces { }: Code blocks
  • Semicolon ; Statement terminator

Important Rules:

  • Class name must match filename
  • Java is case-sensitive
  • Every statement must end with ;
  • main() method is mandatory for execution
  • One public class per .java file

Java Compilation & Execution Process

Java Compilation Process

Step-by-Step Process:

# 1. Compile Java source code
javac HelloWorld.java

# 2. This creates HelloWorld.class (bytecode)
# 3. Execute the bytecode
java HelloWorld

# Output: Hello, World!

Understanding the Compilation Process

1. Source Code (.java)

  • Human-readable Java code
  • Follows Java syntax rules
  • Platform-independent text file

2. Compilation (javac)

  • Syntax checking and validation
  • Converts to bytecode
  • Creates .class file

3. Bytecode (.class)

  • Intermediate representation
  • Platform-independent
  • JVM-specific instructions

4. Execution (java)

  • JVM loads .class file
  • Interprets or compiles bytecode
  • Produces final output

Key Insight: Bytecode makes Java platform-independent. The same .class file can run on Windows, Linux, or macOS without recompilation.

Command Line vs IDE Development

Command Line Development:

# Create source file
notepad HelloWorld.java

# Compile
javac HelloWorld.java

# Run
java HelloWorld

# List files
dir *.class # Windows
ls *.class # Linux/macOS

Advantages:

  • Understand compilation process
  • No tool dependencies
  • Lightweight development

IDE Development:

  1. Create new Java project
  2. Create new Java class
  3. Write code with auto-completion
  4. Click "Run" button
  5. View output in console

Advantages:

  • Auto-compilation and execution
  • Syntax highlighting
  • Error detection
  • Debugging support
  • Project management

Common Beginner Errors & Solutions

Compilation Errors:

1. Class name mismatch

// File: HelloWorld.java
public class Hello { // ❌ Wrong!

Solution: Class name must match filename

2. Missing semicolon

System.out.println("Hello") // ❌ Missing ;

Solution: Add semicolon at end

3. Case sensitivity

system.out.println("Hello"); // ❌ Wrong case

Solution: Use correct case: System

Runtime Errors:

1. Main method not found

Error: Main method not found in class HelloWorld

Solution: Ensure exact signature:
public static void main(String[] args)

2. Class not found

Error: Could not find or load main class HelloWorld

Solution: Check .class file exists and classpath

Debug Tips:

  • Read error messages carefully
  • Check line numbers in errors
  • Verify file and class names match
  • Ensure proper indentation

Previous Year Exam Questions

Q1. (GTU Summer 2022) Write a Java program to display "Hello World" and explain the compilation process.

Solution:

Java Program:

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

Compilation Process:

  1. Source Code: HelloWorld.java (human-readable)
  2. Compilation: javac HelloWorld.java
  3. Bytecode Generation: HelloWorld.class (platform-independent)
  4. Execution: java HelloWorld (JVM interprets bytecode)
  5. Output: "Hello World" displayed on console

Q2. (GTU Winter 2021) Explain the difference between JDK, JRE, and JVM with their components.

Solution:

1. JVM (Java Virtual Machine):

  • Purpose: Executes Java bytecode
  • Components: Class Loader, Execution Engine, Memory Areas
  • Platform-specific but provides platform independence

2. JRE (Java Runtime Environment):

  • Purpose: Environment to run Java applications
  • Components: JVM + Core Libraries + Supporting files
  • For end users who only need to run Java programs

3. JDK (Java Development Kit):

  • Purpose: Complete development environment
  • Components: JRE + Development tools (javac, javadoc, jar, etc.)
  • For developers who write Java programs

Relationship: JDK ⊃ JRE ⊃ JVM

Q3. (GTU Summer 2020) What are the steps to set up Java development environment on Windows? Write the commands to compile and execute a Java program.

Solution:

Java Environment Setup Steps:

  1. Download JDK: From Oracle or OpenJDK website
  2. Install JDK: Run installer as administrator
  3. Set JAVA_HOME: System Properties → Environment Variables
    • Variable: JAVA_HOME, Value: C:\Program Files\Java\jdk-17
  4. Update PATH: Add %JAVA_HOME%\bin to PATH variable
  5. Verify Installation: Open Command Prompt, run:
    java -version
    javac -version

Compilation and Execution Commands:

# Compile Java source file
javac HelloWorld.java

# Execute compiled bytecode
java HelloWorld

# Compile with classpath (if needed)
javac -cp . HelloWorld.java

# Run with classpath
java -cp . HelloWorld

Hands-on Lab Exercise

Exercise 1: Environment Verification

  1. Open command prompt/terminal
  2. Verify Java installation: java -version
  3. Verify compiler: javac -version
  4. Check JAVA_HOME: echo %JAVA_HOME%
  5. Take screenshots of all outputs

Exercise 2: Hello World Variations

  1. Create HelloWorld.java with basic message
  2. Compile and run the program
  3. Modify to print your name and college
  4. Add multiple println statements
  5. Experiment with print vs println
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
        System.out.println("My name is [Your Name]");
        System.out.println("I study at [Your College]");
        System.out.print("Java ");
        System.out.println("Programming is fun!");
    }
}

Assignment & Practice

Programming Assignments:

  1. Personal Introduction:
    • Create a program that displays your personal information
    • Include name, age, college, course, and hobbies
  2. ASCII Art:
    • Create a program that prints ASCII art of your name
    • Use multiple println statements
  3. Environment Documentation:
    • Document your JDK installation process
    • Include screenshots and command outputs
    • Create troubleshooting guide for common issues
  4. IDE Comparison:
    • Try the same Hello World program in 2 different IDEs
    • Compare features and ease of use
    • Write a brief report on your findings

Preparation for Next Lecture:

  • Review Java data types and their ranges
  • Practice writing simple programs with different outputs
  • Experiment with command line compilation and IDE execution

Lecture Summary

Key Concepts Covered:

  • JDK, JRE, JVM relationships
  • Java development environment setup
  • Environment variables configuration
  • First Java program structure
  • Compilation and execution process
  • Command line vs IDE development

Learning Outcomes Achieved:

  • ✅ Installed and configured JDK
  • ✅ Set up development environment
  • ✅ Written first Java program
  • ✅ Understood compilation process
  • ✅ Learned common error handling
  • ✅ Practiced both CLI and IDE development

Next Lecture: Data Types & Variables

Topics: Primitive data types, variable declaration, initialization, type conversion

Thank You!

Questions & Discussion


Next: Lecture 03 - Data Types & Variables


Course: 4343203 Java Programming
Unit 1: Introduction to Java
GTU Semester 4