Java Development Workflow – Write, Compile & Run Your First Program
Learning Java begins with understanding the complete workflow: writing your program, compiling it into bytecode, and running it using the JVM.
This section will guide you step-by-step, including your first Java program, so you can start coding immediately.
Step 1: Write Your First Java Program
Create a Java file with the .java extension. The file name should match the class name.
Example: HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Explanation:
| Part | Description |
|---|---|
public class HelloWorld | Declares a class named HelloWorld |
public static void main(String[] args) | Entry point of the program |
System.out.println("Hello, Java!"); | Prints text to the console |
Every Java program must have a main method to run.
Step 2: Compile Your Program
Compile your source code (.java) into bytecode (.class) using the Java compiler:
Command Line:
javac HelloWorld.java
- On success, this generates
HelloWorld.class. - If there are syntax errors, the compiler will display them with line numbers.
IDE (Eclipse / IntelliJ):
- Click Run or Build; the IDE compiles your program automatically.
Step 3: Run Your Program
Execute the compiled bytecode using the JVM:
Command Line:
java HelloWorld
Output:
Hello, Java!
IDE:
- Eclipse: Right-click → Run As → Java Application
- IntelliJ: Click the green Run button
The JVM reads the bytecode and converts it into machine code for your system.
Step 4: Debug and Iterate
Java development is iterative:
- Write → Compile → Run
- Check for errors → Fix → Recompile → Rerun
Common Issues:
| Issue | Fix |
|---|---|
| Syntax errors | Check spelling, semicolons, and brackets |
| Class name mismatch | Ensure file name matches class name |
| PATH/JDK not set | Configure JAVA_HOME and add bin to PATH |
Optional: Using Build Tools for Larger Projects
For advanced workflows or bigger projects, you can use Maven or Gradle to:
- Automate compilation and execution
- Manage dependencies
- Simplify project structure
Workflow Visualization
1️⃣ Write Java Source Code (.java)
↓
2️⃣ Compile (javac) → Bytecode (.class)
↓
3️⃣ Run (java) → JVM executes bytecode
↓
4️⃣ Output & Debug → Improve Code
Concepts Learned
- Java source code →
.javafile - Compilation → converts source code → bytecode (
.class) - Execution → JVM converts bytecode → machine code
- Main method → program entry point
- Iterative development → write → compile → run → debug
Summary Table
| Step | Action |
|---|---|
| Write Code | Create .java file with main class |
| Compile | Use javac to generate .class bytecode |
| Run | Execute with java command via JVM |
| Debug | Fix errors and iterate |
| Optional | Use Maven/Gradle for larger projects |
🏆 Top 5 Java Interview Questions • Java Workflow: Write, Compile & Run Program
1. What is the complete Java program execution flow?
Answer: The Java program follows these three main steps:
- Write – You create source code in a
.javafile. - Compile – The
javaccompiler converts source code into bytecode (.classfile). - Run – The
javacommand runs the bytecode on the JVM, which converts it into machine code for your operating system.
Example Workflow:
HelloWorld.java → javac HelloWorld.java → HelloWorld.class → java HelloWorld
2. What is the purpose of the javac and java commands?
Answer:
javac(Java Compiler): Converts.javasource files into.classbytecode files.java(Java Interpreter): Runs the compiled bytecode on the JVM, producing output on the console.
3. What is bytecode and why is it important?
Answer:
- Bytecode is an intermediate, platform-independent code generated after compilation.
- It’s stored in
.classfiles and executed by the Java Virtual Machine (JVM). - This bytecode makes Java “Write Once, Run Anywhere” because it can run on any device with a JVM.
4. What is the role of the main() method in Java?
Answer: The main() method is the entry point for any standalone Java application.
public static void main(String[] args)
| Keyword | Meaning |
|---|---|
public | JVM must access it from anywhere |
static | Runs without creating an object |
void | No return value |
String[] args | Accepts command-line arguments |
5. Why must the class name and file name match in Java?
Answer: In Java, if a class is declared as public, the file name must be the same as the class name.
For example:
public class HelloWorld { }
must be saved as
HelloWorld.java
Note: This rule helps the compiler locate and manage public classes correctly during compilation.
