Bird
Raised Fist0
Javaprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What command do you use to compile a Java file named Example.java from the command line?
easy
A. javac Example.java
B. java Example.java
C. compile Example.java
D. run Example.java

Solution

  1. Step 1: Understand Java compilation

    Java source files (.java) must be compiled into bytecode (.class) before running.
  2. Step 2: Use the correct command

    The javac command compiles Java files, so javac Example.java compiles the file.
  3. Final Answer:

    javac Example.java -> Option A
  4. Quick Check:

    Compile Java file = javac [OK]
Hint: Use 'javac' to compile Java files before running [OK]
Common Mistakes:
  • Using 'java' to compile instead of run
  • Typing 'compile' or 'run' commands which don't exist
  • Forgetting to compile before running
2. Which command correctly runs a compiled Java program with the main class named App?
easy
A. java App
B. javac App
C. run App
D. java App.class

Solution

  1. Step 1: Identify how to run Java bytecode

    Compiled Java classes (.class) are run using the java command followed by the class name without extension.
  2. Step 2: Choose the correct syntax

    Running java App executes the App.class file. Adding '.class' or using 'javac' is incorrect here.
  3. Final Answer:

    java App -> Option A
  4. Quick Check:

    Run Java class = java ClassName [OK]
Hint: Run with 'java' plus class name, no file extension [OK]
Common Mistakes:
  • Adding '.class' extension when running
  • Using 'javac' to run instead of compile
  • Typing 'run' command which is invalid
3. Given the commands executed in order:
javac HelloWorld.java
java HelloWorld
What will be the output if HelloWorld.java contains:
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, Java!");
  }
}
medium
A. No output
B. HelloWorld
C. Compilation Error
D. Hello, Java!

Solution

  1. Step 1: Compile the Java file

    The javac HelloWorld.java command compiles the source code into HelloWorld.class without errors.
  2. Step 2: Run the compiled class

    The java HelloWorld command runs the main method, printing the string inside System.out.println.
  3. Final Answer:

    Hello, Java! -> Option D
  4. Quick Check:

    Print statement output = Hello, Java! [OK]
Hint: Compile first, then run to see printed output [OK]
Common Mistakes:
  • Trying to run without compiling first
  • Expecting class name printed instead of message
  • Confusing compile errors with runtime output
4. You try to run java MyProgram but get the error:
Error: Could not find or load main class MyProgram
What is the most likely cause?
medium
A. You ran java MyProgram.java instead of java MyProgram
B. You forgot to compile with javac MyProgram.java
C. The class name inside the file is different from the file name
D. You used the wrong Java version

Solution

  1. Step 1: Understand the error meaning

    This error means Java cannot find the compiled .class file for MyProgram.
  2. Step 2: Check compilation step

    If you did not run javac MyProgram.java first, the .class file does not exist, causing this error.
  3. Final Answer:

    You forgot to compile with javac MyProgram.java -> Option B
  4. Quick Check:

    Compile before run to avoid class not found [OK]
Hint: Always compile before running to avoid class not found errors [OK]
Common Mistakes:
  • Running with .java extension in 'java' command
  • Ignoring class name and file name mismatch
  • Blaming Java version without checking compilation
5. You have two Java files:
File1.java:
public class File1 {
  public static void main(String[] args) {
    System.out.println("File1 running");
  }
}

File2.java:
public class File2 {
  public static void main(String[] args) {
    System.out.println("File2 running");
  }
}

Which sequence of commands correctly compiles and runs File2 from the command line?
hard
A. java File2
javac File2.java
B. javac File2.java
java File2.java
C. javac File2.java
java File2
D. javac File1.java File2.java
java File2.java

Solution

  1. Step 1: Compile the correct file

    To run File2, you must compile File2.java first using javac File2.java.
  2. Step 2: Run the compiled class properly

    Run the program with java File2 (without .java extension). Running before compiling or adding extensions causes errors.
  3. Final Answer:

    javac File2.java java File2 -> Option C
  4. Quick Check:

    Compile then run target class without extension [OK]
Hint: Compile target file, then run class name without extension [OK]
Common Mistakes:
  • Running with .java extension
  • Running before compiling
  • Compiling unrelated files unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard