Java Program Structure, Compilation & Execution, PATH and CLASSPATH
1. Structure of a Java Program
A well-organized Java source file (.java) follows a standard structure divided into 6 sections. Out of these, only the Class Definition with main() method is mandatory for a standalone program.
Figure 1.6: Standard Structure of a Java Program
1Documentation Section
Optional — Comments (//, /*...*/, /**...*/) explaining author, date, and purpose
2Package Statement
Optional — Must be the FIRST non-comment line (e.g., package com.campusnotes;)
3Import Statements
Optional — Imports built-in or user packages (e.g., import java.util.Scanner;)
4Interface Section
Optional — Declares interfaces containing abstract methods and constants
5Class Definitions
Optional — Helper classes (e.g., class Student, class Account)
6Main Method Class
Essential — Contains public static void main(String[] args) where execution starts
- Documentation Section (Suggested):
Contains comments to improve readability. Java supports three types of comments:
- Single-line comment:
// This is a single-line comment - Multi-line comment:
/* This spans multiple lines */ - Documentation comment:
/** Used by javadoc tool to generate HTML API docs */
- Single-line comment:
- Package Statement (Optional):
Declares the folder/namespace to which the classes in this file belong (e.g.,
package college.bca;). If used, it must be the very first statement in the file (before anyimport). - Import Statements (Optional):
Similar to
#includein C++, it instructs the compiler to load classes from other packages (e.g.,import java.io.*;orimport java.util.Scanner;).- Note: The
java.langpackage (which containsString,System,Math,Object,Thread) is imported automatically by default in every Java program!
- Note: The
- Interface Section (Optional): Used to declare interfaces for achieving abstraction and multiple inheritance.
- Class Definitions (Optional): A Java file can contain multiple helper classes that define data models and business logic.
- Main Method Class (Essential):
Every standalone Java application must have at least one class containing the
main()method, as it is the entry point from where the JVM starts program execution.
2. A Simple Java Program & Detailed Breakdown
Let us write a simple Java program, save it as HelloBCA.java, and understand every single word in it:
// 1. Documentation Section
/* File Name : HelloBCA.java
Purpose : First Simple Program in Java */
public class HelloBCA {
// Entry point of the program
public static void main(String[] args) {
System.out.println("Welcome to Java Programming - BCA DAVV!");
}
}
Output:
Welcome to Java Programming - BCA DAVV!
Meaning of Every Keyword in public static void main(String[] args)
This is the #1 Viva and Short-Note Question in BCA Java exams:
public(Access Specifier):main()must be declaredpublicso that the JVM (which sits outside your class) can access and invoke it from anywhere.static(Keyword): When your program starts, no objects of the class exist yet. Declaringmain()asstaticallows the JVM to callHelloBCA.main()directly using the class name, without creating an object ofHelloBCAfirst.void(Return Type): Indicates that themain()method does not return any value back to the JVM when it finishes.main(Method Name): The fixed identifier name that the JVM looks for as the starting point of execution.String[] args(Command-Line Arguments): An array ofStringobjects that receives any arguments passed by the user from the command prompt when launching the program (e.g.,java HelloBCA Indore 2026).
Meaning of System.out.println(…)
System: A predefined final class inside thejava.langpackage (note the capitalS).out: A static reference variable of typePrintStreamdeclared inside theSystemclass, representing the standard output stream (your monitor screen).println(): A method of thePrintStreamclass that prints the text to the console and moves the cursor to a new line.
3. Compilation and Execution of a Java Program
Golden Rule of Naming a .java File
- If a class is declared as
public(e.g.,public class HelloBCA), the source file MUST be saved with the exact same name, including uppercase/lowercase letters:HelloBCA.java. - Why? Because there can be at most one
publicclass per.javafile, and the compiler uses that filename to locate the public class.
Step-by-Step Terminal Commands
- Save the File: Save the code in a file named
HelloBCA.java. - Open Command Prompt / Terminal: Navigate to the folder where
HelloBCA.javais saved. - Compile the Program (
javac):javac HelloBCA.java- The Java compiler (
javac) checks for syntax errors. If there are no errors, it generates a bytecode file namedHelloBCA.classin the same directory.
- The Java compiler (
- Execute the Program (
java):java HelloBCA- Notice: Do not write
.classor.javawith thejavacommand! You pass the Class Name (HelloBCA) to the JVM, which loadsHelloBCA.classand calls itsmain()method.
- Notice: Do not write
4. The Concept of PATH and CLASSPATH
When working with Java from the command line, two environment variables are crucial: PATH and CLASSPATH. Students often confuse them, making this a favorite exam question!
1. What is PATH?
- Purpose:
PATHis an Operating System (OS) environment variable that tells the OS (Windows/Linux) where to find executable tool files (.exebinaries) likejavac.exe(compiler) andjava.exe(launcher). - What happens if
PATHis not set? When you typejavac HelloBCA.javain Command Prompt, Windows will throw the error:'javac' is not recognized as an internal or external command, operable program or batch file. - How to set
PATH( points to JDK’sbindirectory):set PATH=C:\Program Files\Java\jdk-21\bin;%PATH%
2. What is CLASSPATH?
- Purpose:
CLASSPATHis a Java-specific environment variable used by the Java Compiler (javac) and the JVM (java) to locate compiled.classfiles (Bytecode) and external library.jarfiles (like a MySQL JDBC driver) needed by your program. - What happens if
CLASSPATHis not set properly? The JVM will throw a runtime error:Error: Could not find or load main class HelloBCAorjava.lang.ClassNotFoundException. - How to set
CLASSPATH(points to directories containing.classor.jarfiles):
(Note: The dotset CLASSPATH=.;C:\myproject\classes;C:\libs\mysql-connector.jar.represents the current working directory).
5. Comparison: PATH vs CLASSPATH
| Feature | PATH |
CLASSPATH |
|---|---|---|
| 1. Used By | Used by the Operating System (Windows, Linux, macOS). | Used by the Java Compiler (javac) and JVM (java). |
| 2. What It Locates | Locates native JDK executable tools (.exe files) such as javac.exe, java.exe, javadoc.exe, jar.exe. |
Locates user-defined and library .class files (Bytecode) and .jar archives. |
| 3. Target Directory | Points to the jdk\bin folder. |
Points to folders containing .class files or .jar library paths. |
| 4. Error If Missing | 'javac' is not recognized as an internal or external command. |
NoClassDefFoundError or ClassNotFoundException. |
| 5. Can Be Overridden via CLI? | Cannot be overridden by a java flag. |
Can be easily overridden per command using java -cp or java -classpath. |
💡 Exam Preparation Tips for DAVV BCA
- Always remember the 1-line summary for exams:
PATHpoints tojdk/binto find.exetools (javac,java).CLASSPATHpoints to project/library folders to find.classand.jarfiles.
- When explaining
public static void main(String[] args), explain each of the 5 tokens (public,static,void,main,String[] args) in separate bullet points.