Java Environment and Program Structure#
Lecture 2#
Java Programming (4343203)
Diploma in ICT - Semester IV
Gujarat Technological University
layout: default#
Learning Objectives#
By the end of this lecture, you will be able to:
- ๐ง Understand the difference between JVM, JRE, and JDK
- โ๏ธ Explain the bytecode concept and its importance
- ๐๏ธ Describe garbage collection in Java
- ๐ป Install and configure Java development environment
- โจ Write your first “Hello World” Java program
- ๐ Compile and execute Java programs
layout: center#
Java Platform Components#
graph TD
A[Java Development Kit - JDK] --> B[Java Runtime Environment - JRE]
A --> C[Development Tools<br/>javac, javadoc, jar, etc.]
B --> D[Java Virtual Machine - JVM]
B --> E[Java Libraries<br/>API Classes]
D --> F[Class Loader]
D --> G[Bytecode Verifier]
D --> H[Just-In-Time Compiler]
style A fill:#e3f2fd
style B fill:#f3e5f5
style D fill:#fff3e0
style C fill:#e8f5e8
style E fill:#e8f5e8
layout: two-cols#
Java Virtual Machine (JVM)#
๐ฏ What is JVM?#
- Runtime environment for Java bytecode
- Platform-specific (Windows, Linux, macOS)
- Converts bytecode to machine code
- Manages memory automatically
๐ง Key Components#
- Class Loader - Loads .class files
- Bytecode Verifier - Security checks
- Interpreter - Executes bytecode
- JIT Compiler - Optimizes performance
::right::
๐ JVM Architecture#
graph TD
A[.class files] --> B[Class Loader]
B --> C[Method Area]
B --> D[Heap Memory]
C --> E[JIT Compiler]
D --> F[Garbage Collector]
E --> G[Native Method Interface]
F --> H[Operating System]
G --> H
style A fill:#e1f5fe
style C fill:#f3e5f5
style D fill:#e8f5e8
style F fill:#fff3e0
layout: default#
Java Runtime Environment (JRE)#
๐ฏ What is JRE?#
- Runtime environment for Java applications
- Includes JVM + Java libraries
- Required to run Java programs
- Cannot compile Java source code
๐ฆ JRE Components#
- JVM - Virtual machine
- Core Libraries - java.lang, java.util, etc.
- Supporting Files - Property files, resources
- Browser Plugins - For applets (deprecated)
๐ Real-World Analogy
JRE is like a media player - it can play video files (.mp4) but cannot create them. Similarly, JRE can run Java programs (.class) but cannot compile them.
layout: default#
Java Development Kit (JDK)#
๐ ๏ธ What is JDK?#
- Complete development platform
- Includes JRE + development tools
- Required for development
- Free and open source
๐ JDK Versions#
- Java 8 - LTS (Long Term Support)
- Java 11 - LTS
- Java 17 - LTS (Current)
- Java 21 - Latest LTS
๐ง Development Tools#
- javac - Java compiler
- java - Java interpreter
- javadoc - Documentation generator
- jar - Archive tool
- jdb - Debugger
- javap - Class file disassembler
layout: center#
Bytecode Concept#
sequenceDiagram
participant SC as Source Code<br/>(.java)
participant JC as Java Compiler<br/>(javac)
participant BC as Bytecode<br/>(.class)
participant JVM as Java Virtual Machine
participant MC as Machine Code
SC->>JC: HelloWorld.java
JC->>BC: HelloWorld.class
Note over BC: Platform Independent<br/>Intermediate Code
BC->>JVM: Load bytecode
JVM->>MC: Convert to native code
Note over MC: Platform Specific<br/>Executable Code
โ Advantages
- โข Platform independence
- โข Security verification
- โข Optimized execution
- โข Compact representation
โ ๏ธ Characteristics
- โข Not human-readable
- โข Machine-independent
- โข JVM-specific format
- โข .class file extension
layout: default#
Garbage Collection#
๐๏ธ What is Garbage Collection?#
- Automatic memory management
- Removes unused objects
- Prevents memory leaks
- Runs in background
๐ GC Process#
- Mark - Identify unused objects
- Sweep - Remove unused objects
- Compact - Defragment memory
๐พ Memory Areas#
graph TD
A[JVM Memory] --> B[Heap Memory]
A --> C[Non-Heap Memory]
B --> D[Young Generation]
B --> E[Old Generation]
D --> F[Eden Space]
D --> G[Survivor Spaces]
C --> H[Method Area]
C --> I[PC Registers]
C --> J[Native Method Stack]
style B fill:#e8f5e8
style C fill:#fff3e0
style D fill:#e1f5fe
style E fill:#f3e5f5
layout: default#
Installing Java JDK#
๐ฝ Download Sources#
๐ข Oracle JDK
- โข Commercial license
- โข oracle.com/java
- โข Production support
- โข Latest features
๐ OpenJDK
- โข Open source
- โข openjdk.java.net
- โข Community support
- โข Free for all use
โ๏ธ Installation Steps#
- Download JDK installer for your OS
- Run installer with admin privileges
- Set JAVA_HOME environment variable
- Add Java bin directory to PATH
- Verify installation with
java -version
layout: default#
Setting Environment Variables#
๐ช Windows Setup#
# Set JAVA_HOME
JAVA_HOME = C:\Program Files\Java\jdk-17
# Add to PATH
PATH = %JAVA_HOME%\bin;%PATH%
# Verify installation
java -version
javac -version
๐ง Linux/macOS Setup#
# Add to ~/.bashrc or ~/.zshrc
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$JAVA_HOME/bin:$PATH
# Reload configuration
source ~/.bashrc
# Verify installation
java -version
javac -version
layout: default#
Your First Java Program - Professional Deep Dive#
๐ฏ The Professional Hello World#
// HelloWorld.java - Professional Version
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.println("Welcome to Java Programming!");
// Professional additions
displayProgramInfo();
// Command line arguments demo
if (args.length > 0) {
System.out.println("Arguments received: " +
String.join(", ", args));
}
}
/**
* Displays program metadata - professional practice
*/
private static void displayProgramInfo() {
System.out.println("\n=== Program Information ===");
System.out.println("Java Version: " +
System.getProperty("java.version"));
System.out.println("Operating System: " +
System.getProperty("os.name"));
System.out.println("User: " +
System.getProperty("user.name"));
}
}
๐ Professional Code Analysis#
โข **Class declaration** with public modifier
โข **Main method** - application entry point
โข **Helper methods** for code organization
โข **Method extraction** for reusability
โข **System properties** for environment info
โข **Command-line arguments** handling
โข Exploring the Java runtime environment
โข Professional code organization
โข Real-world programming patterns
โข **One public class per file**
โข **main method signature** must be exact
โข **Case sensitivity** throughout Java
๐ From Beginner to Professional in One Program
This isn't just "Hello World" - it's your first step toward writing enterprise-grade Java applications that real companies deploy to serve millions of users!
layout: default#
Compilation and Execution#
โ๏ธ Step-by-Step Process#
1๏ธโฃ Write Source Code
HelloWorld.java - Contains human-readable Java code2๏ธโฃ Compile with javac
javac HelloWorld.java - Creates HelloWorld.class3๏ธโฃ Execute with java
java HelloWorld - Runs the bytecode๐ฅ๏ธ Command Line Demo#
# Navigate to source directory
cd /path/to/your/java/files
# Compile the program
javac HelloWorld.java
# Run the program
java HelloWorld
layout: default#
Complete Development Workflow#
flowchart TD
A[Write Java Code<br/>HelloWorld.java] --> B[Compile with javac<br/>javac HelloWorld.java]
B --> C{Compilation<br/>Successful?}
C -->|No| D[Fix Syntax Errors]
D --> A
C -->|Yes| E[Bytecode Generated<br/>HelloWorld.class]
E --> F[Execute with java<br/>java HelloWorld]
F --> G{Runtime<br/>Errors?}
G -->|Yes| H[Debug & Fix Code]
H --> A
G -->|No| I[Program Output<br/>Hello, World!]
style A fill:#e1f5fe
style E fill:#fff3e0
style I fill:#e8f5e8
style D fill:#ffebee
style H fill:#ffebee
layout: default#
Common Compilation Errors#
โ Syntax Errors#
// Missing semicolon
System.out.println("Hello World")
// Mismatched braces
public class Test {
public static void main(String[] args) {
System.out.println("Hello");
// Missing closing brace
โ Common Mistakes#
// Wrong class name
public class hello { // Should be Hello
// ...
}
// Wrong main method signature
public void main(String[] args) {
// Should be: public static void main
}
// Case sensitivity
system.out.println("Hello");
// Should be: System.out.println
System โ systemlayout: default#
IDE vs Command Line#
๐ฅ๏ธ Command Line Development#
Advantages:
- Direct control over compilation
- Better understanding of process
- Lightweight and fast
- Good for learning
Disadvantages:
- Manual error checking
- No syntax highlighting
- No auto-completion
- More typing required
๐ป IDE Development#
Popular IDEs:
- IntelliJ IDEA (Most popular)
- Eclipse (Free and powerful)
- NetBeans (Oracle supported)
- VS Code (Lightweight)
Benefits:
- Syntax highlighting
- Auto-completion
- Error detection
- Debugging tools
- Project management
layout: default#
Comprehensive Hands-On Lab - Professional Java Setup#
๐ Lab Exercises - Progressive Complexity#
โข Configure JAVA_HOME and PATH variables
โข Verify installation: `java --version` & `javac --version`
โข Document any issues encountered
โข Include: name, enrollment, college, branch
โข Add system information display
โข Handle command-line arguments
โข Execute with arguments: `java StudentInfo arg1 arg2`
โข Debug common errors (syntax, runtime)
โข Organize files in proper directory structure
โข Create new Java project with proper structure
โข Configure JDK in IDE settings
โข Run and debug using IDE tools
๐ Expected Professional Output#
// Expected when running: java StudentInfo "GTU" "ICT"
=== Student Information System ===
Name: Raj Patel
Enrollment: 21ICT001
College: Government Polytechnic
Branch: Information & Communication Technology
=== System Information ===
Java Version: 21.0.1
Operating System: Windows 11
User: raj.patel
Working Directory: C:\JavaProjects\GTU
=== Command Line Arguments ===
Arguments received: GTU, ICT
Argument count: 2
=== Professional Features ===
โ
Proper error handling implemented
โ
Input validation completed
โ
Professional code structure
โ
Documentation standards followed
Thank you for using Student Info System!
Program executed successfully in 0.045 seconds.
๐ Mastery Checklist#
๐ Congratulations! You're Now a Java Developer!
You've successfully set up a professional Java development environment and created your first application.
You're ready to tackle more complex programming challenges and build real-world software solutions!
layout: default#
Troubleshooting Common Issues#
โ 'javac' is not recognized
Solution: Check JAVA_HOME and PATH environment variables
โ Could not find or load main class
Solution: Ensure class name matches filename exactly
โ Public class must be in file named
Solution: Rename file to match public class name
โ Cannot find symbol
Solution: Check spelling and case sensitivity
layout: center class: text-center#
Summary#
๐ What We Learned
- โข JVM, JRE, and JDK concepts
- โข Bytecode and its importance
- โข Garbage collection basics
- โข Java environment setup
- โข First Java program creation
๐ฏ Next Steps
- โข Java program structure details
- โข Types of comments in Java
- โข Coding conventions
- โข More complex programs
- โข Debugging techniques
layout: center class: text-center#
Questions & Discussion#
layout: default#
Java Development Environment Setup#
๐ง Step-by-Step Installation Guide#
Windows Installation#
- Download JDK - Oracle JDK or OpenJDK
- Run Installer - Follow installation wizard
- Set JAVA_HOME - System environment variable
- Update PATH - Add JDK bin directory
- Verify Installation -
java -version
Linux/macOS Installation#
- Package Manager -
sudo apt install openjdk-17-jdk - Homebrew (macOS) -
brew install openjdk@17 - Manual Download - Extract to /usr/local/
- Update Profile - .bashrc or .zshrc
- Verify Setup -
javac -version
layout: default#
IDE Selection and Setup#
๐ IntelliJ IDEA
- โข Intelligent code completion
- โข Powerful debugging tools
- โข Built-in version control
- โข Spring Boot integration
๐ฎ Eclipse
- โข Free and open source
- โข Extensive plugin ecosystem
- โข Good for beginners
- โข Strong community support
๐ VS Code
- โข Lightweight and fast
- โข Java Extension Pack
- โข Git integration
- โข Cross-platform
๐ Alternative Text Editors#
- Notepad++ (Windows) - Simple syntax highlighting
- Sublime Text - Fast with Java packages
- Atom - GitHub’s hackable editor
- Vim/Emacs - For terminal enthusiasts
layout: default#
Java Build Tools Overview#
๐จ Maven#
<project>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</project>
โก Gradle#
plugins {
id 'java'
id 'application'
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
dependencies {
testImplementation 'junit:junit:4.13.2'
}
application {
mainClass = 'com.example.Main'
}
๐๏ธ Build Tool Comparison#
| Feature | Maven | Gradle | Ant |
|---|---|---|---|
| Configuration | XML-based | Groovy/Kotlin DSL | XML-based |
| Performance | Good | Excellent | Good |
| Learning Curve | Moderate | Steep | Easy |
| Ecosystem | Mature | Growing | Legacy |
layout: default#
Java Memory Management Deep Dive#
layout: default#
Advanced JVM Features#
๐ Just-In-Time (JIT) Compilation#
- Interpretation - Initial execution
- C1 Compiler - Client compiler (fast compilation)
- C2 Compiler - Server compiler (aggressive optimization)
- Tiered Compilation - Best of both worlds
- Profile-Guided Optimization - Runtime feedback
- Method Inlining - Eliminate method call overhead
๐ง JVM Tuning Parameters#
# Heap size configuration
-Xms512m -Xmx2g
# Garbage collection
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
# JIT compilation
-XX:+TieredCompilation
-XX:CompileThreshold=10000
# Monitoring and debugging
-XX:+PrintGC
-XX:+HeapDumpOnOutOfMemoryError
layout: default#
Java Development Best Practices#
๐ Project Structure Best Practices#
my-java-project/
โโโ src/
โ โโโ main/
โ โ โโโ java/
โ โ โ โโโ com/company/project/
โ โ โ โโโ Main.java
โ โ โ โโโ model/
โ โ โ โโโ service/
โ โ โ โโโ util/
โ โ โโโ resources/
โ โ โโโ application.properties
โ โ โโโ log4j2.xml
โ โโโ test/
โ โโโ java/
โ โโโ com/company/project/
โโโ target/ (Maven) or build/ (Gradle)
โโโ pom.xml (Maven) or build.gradle
โโโ README.md
โ Do's
- โข Follow package naming conventions
- โข Use meaningful class and method names
- โข Keep classes focused and small
- โข Write unit tests for all methods
- โข Use version control (Git)
โ Don'ts
- โข Don't use default package
- โข Avoid magic numbers and strings
- โข Don't ignore compiler warnings
- โข Avoid deep inheritance hierarchies
- โข Don't commit compiled .class files
layout: default#
Environment Variables and Configuration#
๐ Essential Environment Variables#
JAVA_HOME#
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
PATH#
export PATH=$JAVA_HOME/bin:$PATH
CLASSPATH#
export CLASSPATH=.:$JAVA_HOME/lib/*
JVM Options#
export JAVA_OPTS="-Xms512m -Xmx1g"
โ๏ธ Configuration Files#
Windows (System Variables)#
- Control Panel โ System โ Advanced
- Environment Variables button
- Add or modify system variables
Linux/macOS (~/.bashrc)#
# Java configuration
export JAVA_HOME=/usr/lib/jvm/java-17
export PATH=$JAVA_HOME/bin:$PATH
export MAVEN_HOME=/opt/maven
export PATH=$MAVEN_HOME/bin:$PATH
IDE Configuration#
- Project JDK settings
- Compiler compliance level
- Build path configuration
layout: default#
Troubleshooting Common Issues#
๐จ Installation Problems#
“java command not found”#
- Check JAVA_HOME setting
- Verify PATH configuration
- Restart terminal/IDE
“javac not recognized”#
- Install JDK (not just JRE)
- Add JDK/bin to PATH
- Check system vs user variables
Version conflicts#
- Use
update-alternatives(Linux) - Check multiple Java installations
- Set correct JAVA_HOME
๐ง Runtime Issues#
OutOfMemoryError#
- Increase heap size (-Xmx)
- Check for memory leaks
- Profile application memory
ClassNotFoundException#
- Check CLASSPATH setting
- Verify JAR file locations
- Check package declarations
UnsupportedClassVersionError#
- Compile with correct JDK version
- Match runtime Java version
- Check bytecode compatibility
layout: default#
Performance Monitoring Tools#
๐ JConsole
- โข Built-in JVM monitoring
- โข Memory usage tracking
- โข Thread analysis
- โข MBean inspection
jconsole๐ VisualVM
- โข Profiling capabilities
- โข Heap dump analysis
- โข CPU profiling
- โข Plugin ecosystem
jvisualvmโก JProfiler
- โข Commercial profiler
- โข Advanced analysis
- โข Database profiling
- โข Memory leak detection
jprofiler๐ Key Metrics to Monitor#
- Heap utilization - Memory usage patterns
- GC frequency - Collection overhead
- Thread states - Concurrency issues
- CPU usage - Performance bottlenecks
- Class loading - Startup optimization
layout: center class: text-center#
Summary & Next Steps#
๐ What We Covered
- โข JVM, JRE, JDK architecture
- โข Bytecode and platform independence
- โข Development environment setup
- โข IDE selection and configuration
- โข Build tools and project structure
- โข Memory management concepts
- โข Performance monitoring tools
๐ฏ Ready for Next Lecture
- โข Java development environment working
- โข Understanding of compilation process
- โข Knowledge of memory management
- โข Familiarity with development tools
- โข Project structure best practices
- โข Basic troubleshooting skills
- โข Performance monitoring awareness

