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
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:
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 PATH=$JAVA_HOME/bin:$PATH
2. Reload Profile
# 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:
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.1Common 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):
- Download and install VS Code
- Install "Extension Pack for Java"
- Reload VS Code
- Verify Java detection in status bar
IntelliJ IDEA Setup:
- Download Community Edition
- Install with default settings
- Configure SDK path to JDK
- 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
Step-by-Step Process:
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:
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:
- Create new Java project
- Create new Java class
- Write code with auto-completion
- Click "Run" button
- 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 caseSolution: Use correct case: System
Runtime Errors:
1. Main method not found
Error: Main method not found in class HelloWorldSolution: Ensure exact signature:public static void main(String[] args)
2. Class not found
Error: Could not find or load main class HelloWorldSolution: 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:
- Source Code: HelloWorld.java (human-readable)
- Compilation:
javac HelloWorld.java - Bytecode Generation: HelloWorld.class (platform-independent)
- Execution:
java HelloWorld(JVM interprets bytecode) - 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:
- Download JDK: From Oracle or OpenJDK website
- Install JDK: Run installer as administrator
- Set JAVA_HOME: System Properties → Environment Variables
- Variable: JAVA_HOME, Value: C:\Program Files\Java\jdk-17
- Update PATH: Add %JAVA_HOME%\bin to PATH variable
- 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 . HelloWorldHands-on Lab Exercise
Exercise 1: Environment Verification
- Open command prompt/terminal
- Verify Java installation:
java -version - Verify compiler:
javac -version - Check JAVA_HOME:
echo %JAVA_HOME% - Take screenshots of all outputs
Exercise 2: Hello World Variations
- Create HelloWorld.java with basic message
- Compile and run the program
- Modify to print your name and college
- Add multiple println statements
- 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:
- Personal Introduction:
- Create a program that displays your personal information
- Include name, age, college, course, and hobbies
- ASCII Art:
- Create a program that prints ASCII art of your name
- Use multiple println statements
- Environment Documentation:
- Document your JDK installation process
- Include screenshots and command outputs
- Create troubleshooting guide for common issues
- 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

