⏱️ 7 min read • Beginner Level • Lesson 7
Completed on . You can revise this lesson or continue to the next topic.
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 learning how to write a Java program, you should read Java Program Structure. After this lesson, continue with Java Comments.
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.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.