First Java Program - Compile and Run HelloWorld

⏱️ 7 min read • Beginner Level • Lesson 7

Lesson 7 of 124 of Java Tutorial
You have completed this lesson

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.


What You Need Before Writing Java Program

  • 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.

Steps to Write and Run a Java Program

1. Create the Source Code

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.

Creating HelloWorld Java source file

Example: HelloWorld.java

The following program prints Hello World on the screen:

HelloWorld.java
Copy Try Download
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}   

2. Compile the Program

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:

Change Directory
cd Desktop
Open command prompt to compile Java program

Now compile the Java source file using:

Compile Command
javac HelloWorld.java

After successful compilation, a HelloWorld.class file is generated.

Java compilation process creating class file

3. Run the Java Program

After compilation, run the program using the class name:

Run Command
java HelloWorld
Note: Do not include .class in the execution command. Use java HelloWorld, not java HelloWorld.class.
Run HelloWorld Java program

Explanation of HelloWorld Program

  • public: access modifier that makes the class or method accessible from anywhere.
  • class: keyword used to declare a class.
  • HelloWorld: name of the class. The file name should be HelloWorld.java.
  • static: allows the main method to run without creating an object.
  • void: means the method does not return any value.
  • main: entry point of the Java program.
  • String[] args: stores command-line arguments.
  • System.out.println(): prints output on the console.

Source Code Facts

  • Java source code is written by the programmer.
  • Java source files are saved with the .java extension.
  • A Java source file may contain multiple classes.
  • If a Java source file contains a public class, the file name must match the public class name.
Java Source File Structure
package declaration;
import statements;

class ClassName {
    // class members
}   

Bytecode Facts

  • The Java compiler generates bytecode after successful compilation.
  • Bytecode is stored in a .class file.
  • Bytecode is platform-independent.
  • JVM interprets and runs bytecode.
  • Each compiled Java class generates its own bytecode file.
Java bytecode generation process
Summary:
  • A Java source file is saved with the .java extension.
  • The javac command compiles Java source code.
  • Compilation creates a .class bytecode file.
  • The java command executes the compiled class using JVM.
  • The class name should match the file name if the class is public.

Interview Questions ⭐

You can compile a Java program using the javac command followed by the source file name, for example javac HelloWorld.java.

You can run a compiled Java program using the java command followed by the class name, for example java HelloWorld.

If a Java source file contains a public class, the file name must match the public class name so the compiler can identify the class correctly.

Next step: Learn Java Comments

🚀 Continue to Java Comments →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Writing_executing_java | Language: Java

Question 1 of 10
Q1. In Java, which method serves as the entry point of execution?
Q2. Which of the following is NOT true about Java source code?
Q3. Which command is used to execute a compiled Java program?
Q4. Which statement about bytecode is correct?
Q5. After compiling HelloWorld.java successfully, which file will be generated?
Q6. What is the purpose of 'System.out.println()' in Java?
Q7. Which tool is responsible for interpreting and running Java bytecode?
Q8. Which file extension must be used when saving a Java source code file?
Q9. What is Java bytecode?
Q10. Which command is used to compile a Java program?

🎉 Great job! Continue learning Java step by step.

Discussion

Ask questions, share suggestions, or discuss this lesson.

Please login or create an account to join the discussion and save your learning activity.

Loading comments...

Have you completed this lesson?

Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.

Not completed yet.