We may not have the course you’re looking for. If you enquire or give us a call on 01344203999 and speak to our training experts, we may still be able to help with your training requirements.
We ensure quality, budget-alignment, and timely delivery by our expert instructors.
Used in every nook and corner worldwide, JAVA has gained its status as one of the most widely used and popular programming languages and platforms because of how simple, fast and reliable it is. The easiest program that can be executed in the language is the Java Hello World Program.
‘Hello World’ is the first program every developer compiles when learning a new language.
So, new developers must learn how to compile and run this program. Begin your with our blog and learn how to compile a Java Hello World Program by the end of this blog!
Table of Contents
1) Steps to write and run a Java Hello World Program
a) Step 1: Open Notepad from the start menu
b) Step 2: Create the Java source code
c) Step 3: Save the Java file
d) Step 4: Open the command prompt
e) Step 5: Navigate to the directory
f) Step 6: Compile and run the Java program
g) Step 7: Use an Integrated Development Environment (IDE)
2) Parameters used in Java Hello World Program
3) Conclusion
Steps to write and run a Java Hello World Program
Here are the Steps to write and run a Java Hello World Program:
Step 1: Open Notepad from the start menu
Begin by launching Notepad, a simple text editor that comes pre-installed with Windows Operating Systems. You can open it by navigating to the "Start" button, typing "Notepad" into the search bar, and then hitting Enter. This will open a blank Notepad window where you can write your Java code.
Step 2: Create the Java source code
Within the Notepad window, you can start writing your Java source code. In the case of a "Java Hello World" program, your code will look like this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This code defines a Java class named HelloWorld with a function that prints "Hello, World!" to the console.
Step 3: Save the Java file
After writing the Java code, it's crucial to save it with the ".java" file extension. To do this, go to "File" in the Notepad menu and choose "Save As." Navigate to the directory where you prefer to save the file and provide a name (e.g., HelloWorld.java). Then, ensure that you select "All Files" in the "Save as type" dropdown menu to save it as a text file with the ".java" extension.
Step 4: Open the command prompt
To compile and run your Java program, you need to use the command prompt. Run the command prompt by hitting the "Start" button and then entering "cmd" into the search bar and pressing the Enter key. This will open a command prompt window.
Step 5: Navigate to the directory
You should navigate to the directory where you saved your Java file. You can do this by using the change directory (cd) command. For example, if you saved your file in the "C:workspace" directory, you would enter the following command: "cd C:workspace".
Step 6: Compile and run the Java program
To compile your Java program, you can use the javac command followed by the name of the new Java file without the ".java" extension. For our "Hello World" example, you should enter "javac HelloWorld.java".
If there are no syntax errors in your code, this command will generate a bytecode file with the same name but a ".class" extension (e.g., HelloWorld.class).
To execute the compiled program, use the java command followed by the name of the class (without the ".class" extension): "java HelloWorld". This will run your Java program, and you'll see the output "Hello, World!" displayed in the command prompt.
Step 7: Use an Integrated Development Environment (IDE)
If you prefer a more feature-rich coding environment, you can copy and paste the same Java code into an Integrated Development Environment such as Eclipse, IntelliJ IDEA, or Visual Studio Code. Additionally, after you Install Java on Linux, these IDEs offer advanced tools for coding, debugging, and project management, significantly enhancing your Java development experience. You can use the above steps to write and run a “Java Hello World Program” using a basic text editor and the command prompt.
Hone your Java skills with our Java Engineer Training – sign up today!
Parameters used in Java Hello World Program
By using the program provided above, we can define what the parameters used in the Java Hello World Program are:
import libraries
In Java, import libraries include classes and interfaces defined in other Java packages or modules. Java import statements allow programmers to access pre-existing code in external libraries, thereby avoiding the need to write code from scratch.
To use an external library in the program, the library must first be added to the classpath of the Java Hello World Program. In order to print a simple statement, we do not need to import a complicated external library. Instead, we use "import java.util.*"
java.util contains the legacy collection classes, collections framework, date and time facilities, event model internationalization, and miscellaneous utility classes.
class
In Java, class is a template or blueprint for creating objects.. It defines a set of attributes (data members) and methods (behaviours) common to all objects of that class.
Based on the example above, we define a class called ‘HelloWorld’ that contains a single method called ‘main’. The ‘main’ method is a method in Java that serves as the entry point for the program.
Access modifier
Access modifiers in Java specify the scope or accessibility of a field, method, constructor, or class. We can alter the access level of methods, fields, constructors, and classes by applying access modifiers.
In the example above, the ‘public’ access modifier makes the ‘HelloWorld’ class and the ‘main’ method accessible anywhere in the program.
print statement
We use the print statement in Java to print information to the console. The print statement takes an argument, and arguments are written in between the parentheses.
In the example above, we have used "Hello, World!" as the argument. Double quotation marks surround the argument's words because it informs the compiler that the argument is a string.
Unlock your Web Development potential with our Web Development Using Java Training – sign up now!
Conclusion
Java Hello World is a solid starting step from where programmers can steadily start to explore the vast world of programming. This is mainly because it involves all four primary pillars on which Java is built: import libraries, classes, access modifiers and print statements.
Master the key concept of GUI programming by signing up for Java Swing Development Training.
Frequently Asked Questions
Why use a class and the main method in a “Java Hello World" program?
Java requires code to be written within classes. The main method is the program's entry point, conventionally declared as public static void main(String[] args). It allows the program to start execution.
Can I use any text editor for “Java Hello World" programs?
Yes, any text editor can be used, but it's advisable to use one with code highlighting and auto-completion features for ease and accuracy. Editors like Visual Studio Code or IntelliJ IDEA are popular choices for Java development.
What are the other resources provided by The Knowledge Academy?
The Knowledge Academy takes global learning to new heights, offering over 3,000 online courses across 490+ locations in 190+ countries. This expansive reach ensures accessibility and convenience for learners worldwide.
Alongside our diverse Online Course Catalogue, encompassing 19 major categories, we go the extra mile by providing a plethora of free educational Online Resources like News updates, Blogs, videos, webinars, and interview questions. Tailoring learning experiences further, professionals can maximise value with customisable Course Bundles of TKA.
What is the Knowledge Pass, and how does it work?
The Knowledge Academy’s Knowledge Pass, a prepaid voucher, adds another layer of flexibility, allowing course bookings over a 12-month period. Join us on a journey where education knows no bounds.
What are related courses and blogs provided by The Knowledge Academy?
The Knowledge Academy offers various Java Courses, including Java Programming Course, JavaScript for Beginners and Java Engineer Training. These courses cater to different skill levels, providing comprehensive insights into Java Methodologies.
Our Programming & DevOps Blogs cover a range of topics related to Java, offering valuable resources, best practices, and industry insights. Whether you are a beginner or looking to advance your Java Programming skills, The Knowledge Academy's diverse courses and informative blogs have you covered.
Upcoming Programming & DevOps Resources Batches & Dates
Date
Mon 12th May 2025
Mon 14th Jul 2025
Mon 22nd Sep 2025
Mon 17th Nov 2025