⏱️ 7 min read • Beginner Level • Lesson 3
In this lesson, you will learn how to write, compile, and run your first Java program.
We will create a simple HelloWorld.java file, compile it using the
javac command, and execute it using the java command.
Before starting this lesson, make sure you have completed Java environment setup. If you are new to Java, you can also read Java introduction.
You can write Java code using any text editor like Notepad, Notepad++, VS Code, or gedit.
You can also use Java IDEs such as BlueJ, NetBeans, Eclipse, or IntelliJ IDEA.
To compile and run Java programs, your system must have JDK installed and configured.
Write your Java program in a text editor and save the file with the
.java extension.
In this example, we will create a file named HelloWorld.java.
The following program prints Hello World on the screen:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Open Command Prompt or terminal and go to the folder where your
HelloWorld.java file is saved.
For example, if the file is saved on Desktop, use:
cd Desktop
Now compile the Java source file using:
javac HelloWorld.java
After successful compilation, a HelloWorld.class file is generated.
After compilation, run the program using the class name:
java HelloWorld
.class in the execution command.
Use java HelloWorld, not java HelloWorld.class.
HelloWorld.java..java extension.package declaration;
import statements;
class ClassName {
// class members
}
.class file.
.java extension.javac command compiles Java source code..class bytecode file.java command executes the compiled class using JVM.🧠 Test your understanding with a quick quiz
Topic: Writing_executing_java | Language: Java
🎉 Great job! Continue learning Java step by step.