Learn How to Write and Run Java Hello World Program

Are you a programmer and want to learn or switch to Java programming language? If yes, you are at the right place! Java is a versatile and popular programming language widely known for its flexibility and object-oriented approach.

We will begin with a simple and basic program like “Hello World” to start our Java learning journey. This step-by-step approach will help you enhance your coding skills with time and make Java programming easy and enjoyable.

How to Write Hello World in Java Program

The Java “Hello World” program is a basic introduction to Java programming for beginners. It is a short and straightforward code that will show the message “Hello World” on the console.

This program is often the first step for new Java learners, helping them understand how to write, compile, and run a simple Java program. Moreover, it is a fun way to get started and sets the foundation for exploring more complex Java concepts in the future.

Prerequisites

  • The first thing you have to do before writing any Java program is to set up a development environment. For this purpose, download and install “Java Development Kit (JDK)“.
  • Then, configure the environment variables as it is an essential step to ensure that the Java Development Kit (JDK) and Java Runtime Environment (JRE) are accessible from the command line.
  • Lastly, choose a text editor that suits your requirement for writing Java Code such as Eclipse, IntelliJ IDEA, and Visual Studio (VS Code).
Note: In our case, we have installed Eclipse as it comes with pre-configured Java environment variables.

Now, check out the provided instructions for writing your first Java program.

1. Create a New Java Project

Open your Integrated Environment (IDE) and create or add a new “Java Project” by selecting File > New > Java Project option:

Create a New Java Project
Create a New Java Project

2. Set up Your Java Project Settings

Configure your project settings, such as “Project Name“, “JRE“, and “Project Layout” and click on “Next” and click on “Finish” to proceed next:

Set up Your Java Project Settings
Set up Your Java Project Settings

3. Create Java Package

You can easily create a package by right-clicking the “Project Folder” and then move to New > Package.

Note: A Java program can run without explicitly creating a package. However, it is an ideal practice to maintain code into packages, even for small programs. This maintains code clarity, avoids potential naming conflicts, and promotes code reusability.
Create Java Package
Create Java Package

4. Configure Package Settings

Adjust the package settings, such as “Package Name“, and “Source Folder“. Also, you can mark the highlighted box if you want to “Create package info-java” and click on “Finish” for the next step:

Configure Package Settings
Configure Package Settings

5. Create and Save Java Class

Inside your package, create a new Java class by right-clicking “Package Folder” and then select New > Class:

Create Java Class
Create Java Class

Now, save the Java class by pressing “CTRL + S” or by clicking on “File” and then “Save“:

Create and Save Java Class
Create and Save Java Class

Now, configure the Class settings, such as “Class Name“, “Modifiers” and other necessary settings. Lastly, click on “Finish” to take the final step toward writing the program.

Note: Planning and designing classes in Java offer numerous benefits, including improved code organization, reusability, and early issue detection.
Configure Class Setting
Configure Class Setting

Now, your Java class has been successfully created:

Create Java Class successfully
Create Java Class successfully

6. Write Hello World Program

Finally, you can write your “Hello World” program by following the proper syntax that is given below:

package MyPackage;
public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello World!");
	}
}

Here is the breakdown of the above code:

  • System: It is like a toolbox in Java that has useful tools for doing various operations.
  • out: It is one of the tools in the “System” toolbox that helps us show output on the computer screen.
  • println: It is a specific tool inside “out” that means “print a line“.
  • Hello World!“: It is the text or string that we want to display on the console.
Write Hello World Program
Write Hello World Program

7. Run and View Java Program Output

Now, from the menu bar of your IDE, click on the “Run” option or press “CTRL+F11” to run your Java program:

Run Java Program
Run Java Program

After running the program, you will see the output “Hello World!” displayed in the console or output window of your IDE. This confirms that your Java Hello World program has been successfully executed:

View Java Program Output
View Java Program Output

Congratulations! You have successfully created and run your first Java “Hello World” program. Now, it’s time to explore more exciting Java concepts and build more advanced applications!

Conclusion

Java is a robust and popular programming language that is well-known for its flexibility. You can start your Java learning journey with a simple “Hello World” program that helps you gradually progress your coding skills and makes Java programming interesting.

This journey can lead you to exciting opportunities in software development. Start it with excitement and curiosity and you will uncover the skills of Java Programming.

If you read this far, tweet to the author to show them you care. Tweet a thanks
I am a content writer by passion and by profession. With a background in Computer Science, I've delivered content on various topics including different applications and programming languages.

Each tutorial at GeeksVeda is created by a team of experienced writers so that it meets our high-quality standards.

Join the GeeksVeda Weekly Newsletter (More Than 5,467 Programmers Have Subscribed)
Was this article helpful? Please add a comment to show your appreciation and support.

Got Something to Say? Join the Discussion...