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.
Java compilation and execution flow
javac FileName.java java FileName
javac is the command to compile Java code into bytecode.
java is the command to run the compiled bytecode.
HelloWorld.javac HelloWorld.java java HelloWorld
MyProgram.javac MyProgram.java java MyProgram
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.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, Java!"); } }
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.
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
Solution
Step 1: Understand Java program execution
Java source code must first be compiled into bytecode before running.Step 2: Identify the compilation command
Thejavaccommand compiles source code into bytecode.Final Answer:
Compile the Java source code into bytecode usingjavac-> Option AQuick Check:
Compilation = javac [OK]
- Thinking java command compiles code
- Confusing source code with bytecode
- Skipping compilation step
HelloWorld?Solution
Step 1: Identify the command to run bytecode
Thejavacommand runs compiled bytecode, not source files.Step 2: Match the correct syntax
Usejava HelloWorldwithout the .java extension to run the program.Final Answer:
java HelloWorld -> Option BQuick Check:
Run bytecode = java ClassName [OK]
- Trying to run source file with java command
- Using javac to run program
- Including .java extension when running
javac Test.java java Test
Assuming
Test.java contains:public class Test {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}Solution
Step 1: Compile the source code
javac Test.javacompiles the code into bytecode without errors.Step 2: Run the compiled bytecode
java Testruns the program and prints "Hello Java".Final Answer:
Hello Java -> Option CQuick Check:
javac + java = program output [OK]
- Expecting output without compiling
- Confusing compilation and runtime errors
- Not including main method
java HelloWorld javac HelloWorld.java
Solution
Step 1: Understand command order
You must compile source code before running it.Step 2: Identify error cause
Runningjava HelloWorldbefore compiling causes a runtime error because bytecode is missing.Final Answer:
Running before compiling causes an error -> Option AQuick Check:
Compile first, then run [OK]
- Running java command before javac
- Assuming java compiles automatically
- Ignoring missing .class files
App.java that depends on another class Helper.java. Which sequence correctly compiles and runs the program?Solution
Step 1: Compile dependencies first
CompileHelper.javabeforeApp.javabecause App depends on Helper.Step 2: Run the main program
After compiling both, runjava Appto execute the program.Final Answer:
javac Helper.java javac App.java java App -> Option DQuick Check:
Compile dependencies first, then run main [OK]
- Running before compiling
- Compiling only main class
- Running wrong class with java command
Ready to prove it?
Pass the test and this pattern moves unknown
