Create your First Program in Java
To write code in Java, you must first have Java JDK and a Java Development IDE such as Eclipse or NetBeans, installed on your computer. If you do not have these then please go ahead and install them first.
Let's create a simple Java program to Print Hello World in console. Follow the steps below:
- Launch your Eclipse IDE.
- Create a New Java Project:
- File > New > Project.
- Select Java Project from the project category list and click Next.
- Type your project name in the Project name field as shown in the image below :
- Click the Finish button. It will ask you if you want to open the Java perspective. You can choose to open.
- Create a New Java Class:
- Within your newly created project, right-click on the src folder, from the toolbar select New > package (from the project's context menu).
- Type your package name in the name field. For our example, lets type example.test in the package name field. Then click on Finish.
- Next, right-click on your newly created package and from the available list of options, click on the New > Class.
- On the new New Java Class window, type your Class name in the Name field and then click on Finish.
- Next, let's create the main() method inside the body of the class. The body of a class is enclosed between curly braces { }. Inside the main method, write the code to print the text Hello World on the console as shown below: MyExample.java
- Now, to run your application, right-click on the your class file and from the toolbar options, select Run as > Java Application. If the code is compiled and built successfully then you'll see the following output in console of the eclipse IDE:


The code inside the class should look something like below:
MyExample.java
package example.test;
public class MyExample {
}
Here, the first line of the code is the package name. The package helps to separate one class from another.
The second line of the code is declaration of the Class name followed by curly braces { }. The Class name must always match with the file name in which it is declared.
A Java file must always have atleast one public class, although it can have any number of classes declared but only one class with access modifier public is allowed.
package example.test;
public class MyExample {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
If you do not know what the Java main method is, then we recommend you to read Java Basic Concepts where it is explained in detail.
The System.out.println(" "); will print any String argument that is passed to it.
Note: Every Java Project must have atleast one main method. The main() method is the point from where a java program starts to run the code when a Java application is executed.
Output
Congratulations! You've learned how to create a simple program in Java.