My First Java Program

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:

  1. CMD and Text editor (Notepad, Notepad++)
  2. 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 1 cmd

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 2 cmd

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 same as the class name.

Step 3 cmd

Step 4: Now open your command prompt using Windows+R i.e, run dialog box.

Step 4 cmd

Step 5: After opening cmd, you need to do the following things:

  1. Go to the directory where you had saved the java code using command [cd..] and [cd directory_name]
  2. After that, you can compile your code by typing the following command, javac filename.java [e.g, javac AspiringCoders.java].
Step 5 cmd

Step 6: After 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.

Step 6 cmd

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.

Path can be set like this: set path =C:\Program Files\Java\jdk1.8.0\bin

2. Using IDE Eclipse

You can also write a 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 1 IDE

Step 2: Now create a Java project.

Enter the project name and click on ‘Finish’ button. [E.g. mycode]

Step 2 IDE

Step 3: Your project gets created as shown below. Drop down the project name i.e, mycode and right-click on src.

Step 3 IDE

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 5 IDE

Step 6: Your class is ready. Now write your code for “Hello World”.

And, if you’re new learner then you can copy the first Java program i.e, ‘Hello World’ from the above(at the starting of this page).

Step 6 IDE

Step 7: For executing the code, click on ‘run’ symbol and hit the ‘OK’ button.

Step 7 IDE

Step 8: The output gets printed on the console as ‘Hello World’.

Step 8 IDE

And, the Explanation to the ‘Hello World’ and Java syntax with the execution process will be discussed in the next page.

Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *