Bird
Raised Fist0
Javaprogramming~5 mins

Java compilation and execution flow

Choose your learning style10 modes available
Introduction

Java code needs to be turned into a form the computer can understand and run. This process happens in two main steps: compiling and executing.

When you write a Java program and want to run it on your computer.
When you want to check if your Java code has any errors before running it.
When you want to understand how Java programs start and run behind the scenes.
Syntax
Java
javac FileName.java
java FileName

javac is the command to compile Java code into bytecode.

java is the command to run the compiled bytecode.

Examples
Compile and run a simple program named HelloWorld.
Java
javac HelloWorld.java
java HelloWorld
Compile and run a program named MyProgram.
Java
javac MyProgram.java
java MyProgram
Sample Program

This is a simple Java program that prints a message. First, you compile it with javac HelloWorld.java. Then, you run it with java HelloWorld. The computer first turns the code into bytecode, then runs that bytecode.

Java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}
OutputSuccess
Important Notes

The javac command creates a .class file which contains bytecode.

The java command runs the bytecode using the Java Virtual Machine (JVM).

You must compile your code before running it, or you will get an error.

Summary

Java code is first compiled into bytecode using javac.

The bytecode is then run by the JVM using the java command.

This two-step process helps Java run on many different computers without changing the code.

Practice

(1/5)
1. What is the first step when running a Java program?
easy
A. Compile the Java source code into bytecode using javac
B. Run the bytecode directly using the java command
C. Convert bytecode into machine code manually
D. Write the Java source code in a text editor

Solution

  1. Step 1: Understand Java program execution

    Java source code must first be compiled into bytecode before running.
  2. Step 2: Identify the compilation command

    The javac command compiles source code into bytecode.
  3. Final Answer:

    Compile the Java source code into bytecode using javac -> Option A
  4. Quick Check:

    Compilation = javac [OK]
Hint: Remember: javac compiles, java runs bytecode [OK]
Common Mistakes:
  • Thinking java command compiles code
  • Confusing source code with bytecode
  • Skipping compilation step
2. Which command correctly runs a compiled Java program named HelloWorld?
easy
A. javac HelloWorld.java
B. java HelloWorld
C. java HelloWorld.java
D. javac HelloWorld

Solution

  1. Step 1: Identify the command to run bytecode

    The java command runs compiled bytecode, not source files.
  2. Step 2: Match the correct syntax

    Use java HelloWorld without the .java extension to run the program.
  3. Final Answer:

    java HelloWorld -> Option B
  4. Quick Check:

    Run bytecode = java ClassName [OK]
Hint: Run with java ClassName, no .java extension [OK]
Common Mistakes:
  • Trying to run source file with java command
  • Using javac to run program
  • Including .java extension when running
3. Given the commands below, what will be the output?
javac Test.java
java Test

Assuming Test.java contains:
public class Test {
  public static void main(String[] args) {
    System.out.println("Hello Java");
  }
}
medium
A. Compilation error
B. Runtime error
C. Hello Java
D. No output

Solution

  1. Step 1: Compile the source code

    javac Test.java compiles the code into bytecode without errors.
  2. Step 2: Run the compiled bytecode

    java Test runs the program and prints "Hello Java".
  3. Final Answer:

    Hello Java -> Option C
  4. Quick Check:

    javac + java = program output [OK]
Hint: Compile first, then run to see output [OK]
Common Mistakes:
  • Expecting output without compiling
  • Confusing compilation and runtime errors
  • Not including main method
4. What is wrong with this sequence of commands?
java HelloWorld
javac HelloWorld.java
medium
A. Running before compiling causes an error
B. javac should be run after java
C. Commands are correct and will work
D. HelloWorld.java file is missing

Solution

  1. Step 1: Understand command order

    You must compile source code before running it.
  2. Step 2: Identify error cause

    Running java HelloWorld before compiling causes a runtime error because bytecode is missing.
  3. Final Answer:

    Running before compiling causes an error -> Option A
  4. Quick Check:

    Compile first, then run [OK]
Hint: Always compile before running Java programs [OK]
Common Mistakes:
  • Running java command before javac
  • Assuming java compiles automatically
  • Ignoring missing .class files
5. You have a Java source file App.java that depends on another class Helper.java. Which sequence correctly compiles and runs the program?
hard
A. javac App.java java Helper java App
B. java App javac Helper.java javac App.java
C. javac App.java Helper.java java Helper
D. javac Helper.java javac App.java java App

Solution

  1. Step 1: Compile dependencies first

    Compile Helper.java before App.java because App depends on Helper.
  2. Step 2: Run the main program

    After compiling both, run java App to execute the program.
  3. Final Answer:

    javac Helper.java javac App.java java App -> Option D
  4. Quick Check:

    Compile dependencies first, then run main [OK]
Hint: Compile dependencies before main class, then run main [OK]
Common Mistakes:
  • Running before compiling
  • Compiling only main class
  • Running wrong class with java command
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