This Tutorial Explains How to Create a New Java Project and Run it as a Java Application in the Eclipse IDE:
In the previous tutorial of this Java Eclipse tutorial series, we have learned to Install Eclipse by downloading the Eclipse IDE (Photon) and setting up a workspace for it.
In this tutorial, we will learn all about creating a new Java project in the Eclipse IDE and working with it.
Table of Contents:
Creating and Running Your First Java Project
To create a simple first Java project follow the steps:
#1) Click on File -> New -> Java project.
#2) The following window will open:
Give a name to your project in the highlighted text field. While creating a formal project the name should have a logical sense, however as we are in the learning process, for now, you can name your project as “MyFirstProject” and leave the rest of the fields as default and click next.
#3) The following screen opens up:
Leave all the options as it is and click on finish.
You will see the following structure created in your project explorer.
Here you can see two sub-items under your project name. The JRE system library is the default Java library that Eclipse adds to your project. This library provides Java support and without this, you cannot proceed with creating a Java project. The other folder is the “src” folder or the source folder. Your project structure will get created inside this.
#4) You now have your base project structure, let’s add more to this. In Java, we have packages and classes. Packages provide logical separation for classes. Packages are just folders, whereas classes are where you write your actual code.
Recommended Reading =>> NetBeans Vs Eclipse: Which IDE Is Better For Java Application
To create a package right-click on your project name as shown below.
Click on the package:
Java says package names should be in a small case, so give any name such as “myfirstpackage” and click on finish.
Check your Project Structure.
#5) Next, you need to create a class, and this is where your actual code will go. Right-click on the package name as shown below.
Click on the class:
Give a name to your class and keep the modifier selected as public. While creating a simple Java project, ensure to check the checkbox for creating the method “public static void main(String[] args)”.
Click on finish, and now your project will look as shown below.
You can see that the class is created and as we selected the main method to be added, the method signature is added to the class. Similar to a class you can also create an interface by following the same steps and by selecting “New Interface”.
#6) As we have successfully created a project and class, let’s put a simple print statement in it and run the same to check the output.
Write the below code and save:
Code:
Code generated:
package myfirstpackage; public class MyFirstClass { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("This is my first code"); } }
Once you have saved the file there are multiple places from where you can run it as a Java application.
a) Keep your cursor on the class that you want to run and click on this icon:
b) Right-click on the class name in the project explorer and select Run as and click as shown below.
c) Select the class name in the project explorer and then select the Run option from the top menu -> select Run As and then click on Java application.
#7) As soon as you run your class from any of the above places, a console window opens up in the bottom pane of your workbench and displays the output of your program.
The console in Eclipse displays the runtime logs while your application is running. In a simple application like ours, it is simply the output of the print statement, however, in complex projects, there may be a lot more information displayed.
The console also displays error information if your program encounters any exceptions while running. So in this section, we have created a simple project and seen how we can run it and see the output generated by your code.
Further Reading => Best Java Project Ideas to Look For
Apart from classes, there are many other types of files that you can create in Eclipse which can be used in a Java project such as XML files, interfaces, or even simple files.
You can explore the types of files or types of projects that you can create by clicking on File -> New -> Others, and the following wizard will open up.
However, as a beginner, you may leave these for a while and work with the simple Java project by adding more packages and classes as you need.
Build Your Java Project
A Java builder is the tool that will compile your code and create class files for your classes.
Usually, in Eclipse IDE, the default setting is to have your code build automatically.
However, if you want to build manually, you can uncheck this option and then the Build Project option will be enabled for you. You can also control the build configuration for your project and add or remove builders by doing a right-click on the project name in the project explorer and open properties.
You can add or remove builders from the above section.
However, as a beginner, you may leave these for a while and work with the simple Java project by adding more packages and classes as you need.
Close and Reopen Your Java Project
If you are done working on your project, then it is always a good option to close the project. A closed project will still be available for you in the project explorer section. However, it will not be available in the editor section.
To close a project, simply click on the closed sign on all the open files of that project. To reopen any file, simply navigate to that file from the project explorer and double click on the file name.
Summary
In this tutorial, we learned to create a new and simple Java project in Eclipse and saw how to run it as a Java application.
We also saw that there are several types of files like XML’s that you can create from Eclipse. Similarly, as you explore more, you will find many options that you can keep adding to your project.
In our upcoming tutorial, we will take a look at the common features that Eclipse offers to help you code in a better manner.