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.
0
0
Java compilation and execution flow
Introduction
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.