Structure of a Simple Java Program, CLI Compilation, PATH & JDK vs JRE vs JVM
1. Topic Introduction
Every Java application—from a three-line exam snippet to a banking backend—follows a strict structural skeleton and passes through two distinct phases: compilation (javac) and runtime execution (java). Understanding what each keyword in public static void main(String[] args) does and how JDK, JRE, and JVM relate guarantees easy 3-mark and 6-mark theory points.
Think of the JDK as a complete culinary studio: it contains recipes, knives, ovens, and a chef (compiler + debugger + runtime). If a friend only wants to heat and eat pre-cooked meals (already compiled .class files) without cooking from scratch, they only need the JRE (microwave + utensils). Inside that JRE is the JVM—the heating core that actually converts instructions into warm food.
javac, javadoc, javap). JRE = JVM + Core Class Libraries (java.lang, java.util).3. Essential Exam Definitions
javac, javap, javadoc, jdb).java, not javac).javac).java source files for syntax, semantic, and type errors and translates valid source into .class bytecode.PATH tells the OS shell where JDK executables (javac, java) reside; CLASSPATH tells javac and the JVM where user-defined .class files and third-party .jar libraries are located.4. Detailed Technical Explanation & Syntax
Anatomy of a Simple Java Program
A standard Java source file follows an ordered top-to-bottom layout: optional package statement -> optional import statements -> class declaration -> fields, constructors, and methods. Execution of a standalone Java application always begins at main().
public: Access modifier makingmainvisible to the JVM outside the class/package.static: Allows the JVM to invokemain()directly using the class name before any object ofHelloKTUis created.void: Indicatesmain()returns no value back to the JVM.main: Fixed method identifier searched for by the JVM launcher as the entry point.String[] args(orString args[]): Array capturing command-line arguments passed as strings when starting the program.System.out.println(...):Systemis a final class injava.lang;outis apublic static final PrintStreamfield;println()prints text followed by a newline.
| // 1. Optional package statement |
| // 2. Optional import statements (java.lang is imported automatically) |
| public class HelloKTU { |
| public static void main(String[] args) { |
| System.out.println("Welcome to KTU OOP!"); |
| } |
| } |
Command-Line Compilation & Execution Workflow
When working on the command line (or behind the scenes in an IDE), two separate commands handle compile time and runtime:
- Compile Time (
javac HelloKTU.java): Note the.javafile extension is REQUIRED.javacchecks syntax and type safety. Any syntax violation (e.g., missing;or mismatched types) aborts compilation and prints a compile-time error with line number. Success emitsHelloKTU.class. - Runtime (
java HelloKTU): Note you pass the CLASS NAME WITHOUT.class! The OS usesPATHto findjava, which starts the JVM. The JVM usesCLASSPATH(default.current directory) to findHelloKTU.class, verifies bytecode, and callsHelloKTU.main(). - Runtime Errors vs Compile-Time Errors: Syntax errors happen at compile time (
javac). Logic failures like division by zero (10 / 0) compile cleanly and fail at runtime (ArithmeticException).
Programming Environment: Command Line vs IDE Concept
Students can write Java using a simple text editor + terminal (javac/java) or an Integrated Development Environment (IDE) like Eclipse, IntelliJ IDEA, or NetBeans. An IDE automates calling javac and java under the hood, manages CLASSPATH, and highlights syntax errors early—yet the underlying JDK compilation and JRE execution pipeline remains identical.
5. Working Principle: Command-Line Lifecycle From Source to Output
- Save source code in
HelloKTU.javamatching thepublic class HelloKTUname. - Run
javac HelloKTU.java— OS locatesjavacviaPATH; compiler validates syntax and producesHelloKTU.class. - Run
java HelloKTU— OS launchesjava; JVM searchesCLASSPATH(default.), loadsHelloKTU.class, and verifies bytecode. - JVM calls
HelloKTU.main(String[] args)without constructing aHelloKTUobject becausemainisstatic.
6. Examination-Ready Diagrams
Java Program Compilation and Execution
Two-stage hybrid compilation and interpretation flow demonstrating Write Once, Run Anywhere (WORA) across Windows, Linux, and macOS.
- Source Code (.java): Human-readable Java class file written by the programmer.
- Java Compiler (javac): Checks syntax/types and translates
.javainto platform-independent bytecode. - Bytecode (.class): Intermediate binary instructions executed by any JVM.
- Platform-Specific JVM: Windows JVM, Linux JVM, or macOS JVM interprets/JIT-compiles bytecode into native machine code.
- Write source file e.g.
HelloWorld.java. - Run
javac HelloWorld.javato compile source intoHelloWorld.class(Bytecode). - Run
java HelloWorldto launch the OS-specific JVM. - JVM loads bytecode, verifies it, and translates it via Interpreter + JIT into host CPU machine code.
- Use a clean left-to-right pipeline:
.java->[javac Compiler]->.class (Bytecode). - Branch
.class (Bytecode)into 3 boxes:JVM (Windows),JVM (Linux),JVM (macOS)->Machine Code. - Label
.classclearly as 'Platform Independent Bytecode (WORA)'.
7. Complete Java Code Examples & Tracing
| 1 | // Single-line comment: Structure of a simple Java program |
| 2 | import java.lang.Math; // Optional since java.lang is auto-imported |
| 3 | |
| 4 | public class ProgramStructureDemo { |
| 5 | static String course = "KTU OOP Concepts"; |
| 6 | |
| 7 | static void printHeader() { |
| 8 | System.out.println("=== " + course + " ==="); |
| 9 | } |
| 10 | |
| 11 | public static void main(String[] args) { |
| 12 | printHeader(); |
| 13 | int maxMarks = 60; |
| 14 | double passingScore = Math.ceil(maxMarks * 0.40); |
| 15 | System.out.println("ESE Max Marks: " + maxMarks); |
| 16 | System.out.println("Minimum 40% Reference: " + passingScore); |
| 17 | } |
| 18 | } |
=== KTU OOP Concepts === ESE Max Marks: 60 Minimum 40% Reference: 24.0
importpublicclassstaticvoidmainString- Running
java ProgramStructureDemo.classinstead ofjava ProgramStructureDemo(causes ClassNotFoundException). - Omitting
staticonmain()— the program compiles, but JVM throwsMain method is not staticat runtime. - Writing
Classwith uppercase C instead of lowercaseclass.
Comparison Tables
| Criteria | JDK (Java Development Kit) | JRE (Java Runtime Environment) | JVM (Java Virtual Machine) |
|---|---|---|---|
| Full Form | Java Development Kit | Java Runtime Environment | Java Virtual Machine |
| Role | Compile, debug & run Java apps | Run compiled Java apps only | Load, verify & execute bytecode |
| Physical Existence | Installed physically on disk | Installed physically on disk | Virtual runtime engine inside JRE |
| Tools Included | javac, java, javap, javadoc, JRE | java, runtime libraries (no javac) | No CLI tools; executes bytecode |
| Relationship | Superset of JRE | Subset of JDK, Superset of JVM | Core execution subset of JRE |
| Feature | PATH | CLASSPATH |
|---|---|---|
| Used By | Operating System shell (CMD/Bash) | Java Compiler (javac) & JVM (java) |
| Points To | JDK bin folder (javac, java binaries) | User .class root folders and .jar files |
| Default | OS system/user binary folders | Current working directory (.) |
Real-World Applications
- Build pipelines and continuous integration servers invoke
javacandjavadirectly via CLI. - Servers running production Java apps only require a JRE/runtime image rather than full development tools.
Important KTU Exam Points
- Memorize what every token in
public static void main(String[] args)means — extremely common 3-mark question. - Always write
JDK = JRE + Development ToolsandJRE = JVM + Runtime Libraries. - Contrast
javac File.java(requires.java) vsjava ClassName(no.class).
Memory Techniques & Revision
- PSVM:
public(JVM can see it),static(no object needed),void(returns nothing),main(entry name). - Subset Nesting:
JVM ⊂ JRE ⊂ JDK.
Common Pitfalls to Avoid
- Confusing
PATH(for OS findingjavac.exe) withCLASSPATH(for JVM finding.classfiles). - Thinking
voidmeansmainaccepts no parameters —voidis the RETURN type;(String[] args)is the parameter list.
KTU-Style Questions & Exam-Ready Answers
Structured specifically for KTU Part A (3-mark) and Part B (6/9-mark) evaluation rubrics.
public: Allows the JVM (located outside the class/package) to access and invokemain.static: Enables the JVM to callmaindirectly via the class name without creating an object first.void: Specifies thatmainreturns no value to the JVM upon completion.main(String[] args): Standard entry-point identifier whereargsreceives command-line strings.
Quick Understanding Check
1. Which command correctly compiles and then runs a file containing public class Exam?
2. What happens if main is declared without the static keyword?