C

Core Java tutorial for beginners

Clean • Professional

Java Development Workflow: Write, Compile & Run Your First Program

2 minute

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:

PartDescription
public class HelloWorldDeclares 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:

  1. Write → Compile → Run
  2. Check for errors → Fix → Recompile → Rerun

Common Issues:

IssueFix
Syntax errorsCheck spelling, semicolons, and brackets
Class name mismatchEnsure file name matches class name
PATH/JDK not setConfigure 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 → .java file
  • 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

StepAction
Write CodeCreate .java file with main class
CompileUse javac to generate .class bytecode
RunExecute with java command via JVM
DebugFix errors and iterate
OptionalUse 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:

  1. Write – You create source code in a .java file.
  2. Compile – The javac compiler converts source code into bytecode (.class file).
  3. Run – The java command 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 .java source files into .class bytecode 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 .class files 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)
KeywordMeaning
publicJVM must access it from anywhere
staticRuns without creating an object
voidNo return value
String[] argsAccepts 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.

Article 0 of 0