☕ How to Compile and Run Your First Java Program (Complete Guide)
"An investment in knowledge pays the best interest." — Benjamin Franklin
This guide is a step-by-step tutorial for beginners to understand how to set up Java, write their first program, and successfully compile and run it using Command Prompt (CMD). It includes expert-level insights and interview-related questions to boost your core understanding.
✅ Step 1: Install Required Software
Before you start coding in Java, ensure you’ve completed the following:
-
✅ Installed Java Development Kit (JDK)
-
✅ Chosen a code editor (like Notepad++, VS Code) or IDE (like IntelliJ or Eclipse)
✅ Step 2: Create Your First Java Program
🔸 Java Naming Conventions (Best Practices)
| Element | Naming Convention Example |
|---|---|
| Class Name | MyFirstProgram, StudentInfo |
| Method Name | getDetails(), calculateSum() |
| Variable Name | userName, totalMarks |
| Constant Name | MAX_VALUE, PI |
| File Name | Should match the public class name |
🔸 Structure of a Java Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Explanation:
-
public class HelloWorld— Class declaration -
public static void main(String[] args)— Main method; the entry point of the Java program -
System.out.println()— Used to print output to the console
✅ Step 3: Compile & Run Java Program Using CMD
🔹 Step-by-Step Commands
-
Open Command Prompt
-
Check if Java is installed
Run the following command:javac-
If you see an error like
'javac' is not recognized..., then the Java path is not set.
-
🔹 How to Set Java Path
There are two ways to set the Java path:
➤ Temporary Path (for current CMD session only)
set path=C:\Program Files\Java\jdk-XX.X.X\bin
➤ Permanent Path (recommended for long-term use)
-
Open Environment Variables in your system settings
-
Under System Variables, find
Pathand click Edit -
Add:
C:\Program Files\Java\jdk-XX.X.X\bin
🔹 Compile the Java Program
Navigate to the folder where your .java file is saved:
cd path\to\your\file
Then compile the program:
javac FileName.java
✅ A
.classfile will be generated upon successful compilation.
🔹 Run the Java Program
java ClassName
Note: Use the class name (not the file name) to run the program.
🔍 Important Rules and Cases
⚠️ Case 1: File Name vs Class Name
-
If your class is
public, the file name must match the class name -
You can have multiple classes in one
.javafile, but only one public class -
If there’s no public class, you can name the file freely
🧠 Multiple classes = multiple .class files generated during compilation
⚠️ Case 2: Variations of main() Method
The following declarations are valid in Java:
public static void main(String[] args)
static public void main(String[] args)
public static void main(String args[])
public static void main(String xyz[])
public static void main(String... args)
synchronized final public static void main(String[] args)
❌ Invalid Example:
public void static main(String[] args)
// Invalid due to incorrect sequence of return type and method name
🧠 Common Interview Questions (With Answers)
1. How to Set Path in Java?
-
Temporary Path: Using CMD
set path=...— valid only for the current session -
Permanent Path: Set through system environment variables — stays forever
2. What’s the Difference Between Temporary and Permanent Path?
| Feature | Temporary Path | Permanent Path |
|---|---|---|
| Duration | Active during current session | Works across system restarts |
| How to Set | Using CMD | Via Environment Variables settings |
| Preferred For | Quick testing | Long-term development |
3. What Happens During Java Compilation?
-
.javafile is converted into bytecode -
The compiler (
javac) generates a.classfile -
The JVM (Java Virtual Machine) later executes this bytecode
4. What Are the Different Valid Ways to Declare main() Method?
See the earlier section — there are multiple syntactically valid variations, but the preferred and most standard one is:
public static void main(String[] args)
✅ Conclusion
-
You’ve now learned how to write, compile, and run a basic Java program
-
This foundational knowledge will help you in interviews and in developing more complex applications
-
Understanding the Java execution model (compiler + JVM) is critical for backend and enterprise Java roles
Comments
Post a Comment