Skip to main content

☕ How to Compile and Run Your First Java Program (Complete Guide) || FSJD-08-29-05

 

☕ 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

  1. Open Command Prompt

  2. 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 Path and 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 .class file 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 .java file, 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?

  • .java file is converted into bytecode

  • The compiler (javac) generates a .class file

  • 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

Popular posts from this blog

What is Java? A Complete Beginner Guide to Java as a Language and Platform || FSJD-01-24-05

☕ What is Java? A Beginner-Friendly Guide to Java as a Language and Platform Are you just stepping into the world of programming? Or preparing for that first tech interview and trying to make sense of Java? Don’t worry — you're in the right place. Java isn’t just a coding language; it’s a whole ecosystem. In this beginner-friendly post, we’ll break down what Java really is — in simple terms — so you can start your learning journey with clarity and confidence. 🧠 So… What Exactly is Java? Java is both a programming language AND a platform . Yes — it’s more than just lines of code. Java provides a complete environment that lets you build applications for almost any device — from laptops to smartphones to smart TVs and more. 💻 Java as a Programming Language Java was created by James Gosling and his team at Sun Microsystems (now owned by Oracle ) and officially released in 1995 . Let’s break down what makes Java special as a language: ✅ High-Level Easy to read and wr...

☕ Understanding the Structure of a Java Program – Documentation || FSJD-07-29-05

☕ Understanding the Structure of a Java Program – A Beginner to Pro Guide “An investment in knowledge pays the best interest.” — Benjamin Franklin If you’re starting your journey in Java, the structure of a Java program is one of the first and most important things you should understand. Whether you're preparing for interviews or writing your first piece of code, a strong foundation can set you up for success. In this blog, we’ll break down the complete anatomy of a Java program — from the documentation section to the main() method — and explain each component in simple yet professional terms. 📋 Step-by-Step: How to Prepare Your First Java Program Before writing a Java program, follow these key steps: Download & Install Java Software Install the Java Development Kit (JDK) Choose an editor or IDE (like VS Code , IntelliJ , or Eclipse ) Understand the Java Program Structure Learn Java naming conventions Understand different sections o...

🔰 Java Software Setup Guide for Beginners || FSJD-05-29-05

  🔰 Java Software Setup Guide for Beginners By: Tahir | For Interview Prep & Fresh Learning “An investment in knowledge pays the best interest.” – Benjamin Franklin Whether you’re starting out with Java or brushing up for interviews, setting up the right environment is your first and most important step. In this post, I’ll walk you through the essential tools and software needed to start coding in Java, using simple language and clear concepts. 🚀 What Do You Need to Start Java Programming? To begin Java development, you need three main things: 1. JDK (Java Development Kit) The JDK is the core software you need to write, compile, and run Java programs. ✅ What Is JDK? The Java Development Kit is a package of tools that allows developers to write Java programs. It includes: A compiler ( javac ) to convert Java code into bytecode The Java Runtime Environment (JRE) to run the bytecode Development tools and libraries 📌 Important JDK Versions: Java S...