Creating a ‘Hello World’ program in Java is not a single-line code. It consists of several lines of code.
A ‘Hello World’ program simply prints the ‘Hello World’ as an output on the screen. So, this code is often used to introduce the programming language to a new learner.
To write a Java program first you need to install the JDK. Click on the given link to install JDK.
After that, you can write a Java program using:
- CMD and a Text editor (Notepad, Notepad++)
- or, IDE like Eclipse, NetBeans, IntelliJ IDEA etc.
1. Using cmd and Text editor
Step 1: Open the notepad from the Start menu or direct by pressing Windows+R. Press Windows+R to open the “run” box, type ‘notepad’ and then click on ‘OK’ button.
Step 2: Write source code for your ‘Hello World’ program. Or simply copy the code provided below.
public class AspiringCoders
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
Step 3: After writing the Java program, save it as filename.java [e.g. AspiringCoders.java]. You may be wondering why we save with this name, the thing is that always save the file name as the same as the class name.
Step 4: Now open your command prompt using Windows+R i.e, run dialog box.
Step 5: After opening cmd, you need to do the following things:
- Go to the directory where you had saved the Java code using commands [cd..] and [cd directory_name]
- After that, you can compile your code by typing the following command, javac filename.java [e.g. javac AspiringCoders.java].
Step 6: After the successful compilation of code, the .java file gets translated into the .class file(byte code). And, to run the program type the command: java filename [e.g. java AspiringCoders].
The output i.e. “Hello World” gets printed on your command prompt screen.
Note: You may get an error when you try to compile your code, “javac is not recognized as an internal or external command, operable program or batch file”.
This error occurs when the Java path is not added to the path environment variable. For that, you need to set the path first to solve this error.
The path can be set like this: set path =C:\Program Files\Java\jdk1.8.0\bin
2. Using IDE Eclipse
You can also write Java code in Java IDE like Eclipse, Netbeans etc. It makes the execution easy as compilation and execution can be done in one click and improves your efficiency.
If you are a beginner then I recommend you to use the text editor for better understanding.
First, you need to download and install the Java IDE, Eclipse
Open your browser and type: https://www.eclipse.org/
After proper installation of the eclipse, the user can start their IDE using the following steps:
Step 1: Open the Eclipse IDE. Click on the “Create a new Java project” option.
Step 2: Now create a Java project.
Enter the project name and click on the ‘Finish’ button. [E.g. mycode]
Step 3: Your project gets created as shown below. Drop down the project name i.e. mycode and right-click on src.
Step 4: Do click on src >new >class.
Step 5: Create a Java Class.
Enter the Name i.e. AspiringCoders.
Check out the ‘public static void main(String[] args)’ method. And, simply hit the finish button.
Step 6: Your class is ready. Now write your code for “Hello World”.
And, if you’re a new learner then you can copy the first Java program i.e. ‘Hello World’ from the above(at the start of this page).
Step 7: For executing the code, click on ‘run’ symbol and hit the ‘OK’ button.
Step 8: The output gets printed on the console as ‘Hello World’.
And, the Explanation of the ‘Hello World’ and Java syntax with the execution process will be discussed on the next page.
Leave a Reply