0
0
Javaprogramming~10 mins

Command line execution in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Command line execution
Write Java code in .java file
Compile with javac
Generate .class bytecode file
Run with java command
JVM executes bytecode
Program output shown in terminal
This flow shows how Java source code is compiled and then run from the command line, producing output in the terminal.
Execution Sample
Java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}
This Java program prints 'Hello, world!' to the terminal when run from the command line.
Execution Table
StepCommandActionResult
1javac Hello.javaCompile source codeGenerates Hello.class bytecode file
2java HelloRun bytecode with JVMJVM starts and calls main method
3System.out.printlnPrint text to terminalOutputs: Hello, world!
4Program endsJVM exitsTerminal returns to prompt
💡 Program ends after main method completes, JVM exits and terminal is ready for next command
Variable Tracker
VariableStartAfter Step 2After Step 3Final
argsempty arrayempty arrayempty arrayempty array
Key Moments - 3 Insights
Why do we need to compile the Java file before running it?
Java source code (.java) must be compiled into bytecode (.class) before running. Step 1 shows compilation creating Hello.class, which the JVM runs in Step 2.
What does the 'java Hello' command do?
It starts the JVM and runs the main method in Hello.class. This is shown in Step 2 where the JVM executes the bytecode.
Why does the program output appear in the terminal?
System.out.println sends text to standard output, which is the terminal. Step 3 shows this output action.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what file is created after running 'javac Hello.java'?
AHello.class
BHello.java
CHello.txt
DHello.exe
💡 Hint
Check Step 1 in the execution table where compilation generates the .class file
At which step does the program print 'Hello, world!' to the terminal?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at Step 3 where System.out.println outputs text
If you skip the 'javac' compilation step, what happens when you run 'java Hello'?
AProgram runs normally
BProgram prints nothing but exits
CJVM shows an error: class not found
DTerminal closes automatically
💡 Hint
Refer to Step 1 and 2: compilation creates .class file needed by JVM in Step 2
Concept Snapshot
Java command line execution steps:
1. Write code in Hello.java
2. Compile with 'javac Hello.java' to create Hello.class
3. Run with 'java Hello' to start JVM
4. JVM runs main method and prints output
5. Program ends and terminal returns to prompt
Full Transcript
This visual trace shows how Java programs run from the command line. First, you write your code in a file named Hello.java. Then, you compile it using the command 'javac Hello.java', which creates a Hello.class file containing bytecode. Next, you run the program with 'java Hello', which starts the Java Virtual Machine (JVM). The JVM executes the main method inside Hello.class. When the program calls System.out.println, it prints 'Hello, world!' to the terminal. After the main method finishes, the JVM exits and the terminal is ready for new commands. If you try to run without compiling first, the JVM will give an error because it cannot find the class file. This step-by-step flow helps beginners understand the commands and what happens behind the scenes when running Java programs from the command line.