Creating Your First Java Project in Eclipse
A workspace is the directory where your work is stored, and you can use several workspaces and choose one at startup.
Where Your Eclipse Work Is Kept
Imagine you have just installed Eclipse and written your first Java program. When you close the program, where does that work go, and how would you find it again next week? Eclipse answers this with a single idea: every piece of work you do in Eclipse is kept inside one place on your computer. Understanding that place, and the way the Eclipse window is arranged around your work, is the foundation for everything you will do in the environment.
This article follows the path a learner takes in three steps. First comes the workspace, the place where work is stored. Second comes the window, which is made of sections holding views. Third come the perspectives, which group those views for different jobs. After that, you will walk through creating a Java project, the container that holds the source files for one program. Read this with a real Eclipse window open so you can check each step as you go.
Choosing a Workspace at Startup
A workspace is the directory where your work is stored. You can use several workspaces and choose one at startup.
A workspace is not something Eclipse creates silently in the background. The first time you start Eclipse, it asks you to specify a workspace. You can accept the default name that is offered, or type a name of your own. The choice matters because the workspace is the directory where your work is kept, so the name you pick tells you where to look later.
One installation of Eclipse is not limited to one workspace. You can keep, for example, one workspace for coursework and another for a separate project, then choose between them each time you launch the program. The workspace you select at startup is the one whose contents appear in the environment for that session.
Suppose you keep a coursework workspace and a personal workspace. Each time you launch Eclipse, the startup choice decides which set of projects you see. Choosing the coursework workspace shows only coursework projects for that session; choosing the other shows only the personal ones.
Sections, Views, and the Window Layout
The Eclipse graphical user interface is best understood as one large window that has been divided into several sections. Each section is not empty space waiting to be filled; it holds one or more views. A view is the component that actually shows you something. Read the window from the outside in: the window contains sections, and each section contains views.
The source does not fix which view appears in which section; it only says that each section holds one or more views. What it does specify is the kinds of view you will meet: a text editor, a place where a program can do input and output, and a list of your projects. Because each view has a different job, the window needs several of them at once, and that is why it is divided into sections in the first place.
Perspectives Group the Views
Views are the individual panels. A perspective is the whole set of views in the window, meaning one arrangement of those panels that suits a particular job. Changing perspective changes which views make up the window, which in turn changes what the window is good for at that moment. The Java Perspective is the default for compiling and running programs. The Debug Perspective helps you find semantic errors, which are errors in what a program means or does rather than in its surface form.
| Aspect | View | Perspective |
|---|---|---|
| What it is | A single panel that displays one type of information | The whole set of views in the window, arranged for a job |
| Examples from the source | Text editor, I/O area, project list | Java Perspective, Debug Perspective |
| Effect of a change | Shows or hides one panel's content | Changes which views make up the window |
| Typical use | Showing source code, program output, or projects | Compiling and running, or finding semantic errors |
Views and perspectives as described in the source material
Creating the Project Through the Wizard
Before you can write a single line of Java in Eclipse, you have to decide where that code will live. Eclipse does not treat a loose source file as a starting point. All of your work is organized inside a container called a project. A project gathers the source files, the library settings, and the other resources a program needs, so the IDE always knows what belongs to which program.
- 1Open the File menu.
- 2Open the New submenu.
- 3Choose the Java Project command. A window opens in which you describe the project before Eclipse creates it.
- 4Fill in the Project Name. Eclipse allows any name, so choose one that identifies the program, because you will see that name in the project list later.
- 5Check the JRE and Module settings in the wizard. These are covered in the next section.
- 6Click Finish. The project only appears in the Package Explorer after Finish is clicked, not while the window is still open.
Setting the JRE and Module Options
The wizard has two sections beyond the name that need your attention. One controls which Java environment the project runs against, and the other controls whether the project is treated as a modular program. Eclipse remembers the JRE and Module settings from earlier, so you should check them again for each new project rather than trusting the values shown from last time.
Reading the New Project Tree
After you click Finish, the new project appears in the Package Explorer view on the left side of the Eclipse window. The project is a folder-like entry you can expand by clicking the small triangle or plus sign beside its name. With the default settings, two items matter most: the src directory and the JRE System Library.
The two items play different roles. The src directory is where you write. When you later add a class, it belongs inside src. The JRE System Library is not something you write; it is the collection of standard built-in classes that come with Java, controlled by the JRE setting you chose in the wizard.
Creating a project named Hello and adding a class
Create a Java project called Hello in Eclipse and add a class that prints a greeting.
- 1Open the wizard
Choose File, then New, then Java Project.
- 2Name the project
Type Hello as the Project Name. Any name is allowed, but this one identifies the program.
- 3Check the settings
Set the execution environment to JavaSE 17 or later and uncheck 'Create
module-info.javafile' for this non-modular program. - 4Finish
Click Finish. Hello appears in Package Explorer with a src directory and a JRE System Library.
- 5Add the class
Place a new class inside the src directory, since that is where source code belongs.
A Hello project with src holding your class, and the JRE System Library holding the standard built-in classes.
Hello from my first Eclipse project- The class is named Hello and sits inside the src directory of the Hello project.
- The main method is where a Java program starts running.
System.out.printlnwrites the text to the console output.
Mistakes When Starting a Project
Common mistake Assuming the JRE and Module settings carry over unchanged
Why it failsEclipse remembers these settings, so the values shown may come from an earlier project.
Do this insteadCheck the JRE and Module settings in the wizard again for every new project.
Common mistake Leaving the project's source code outside the src directory
Why it failssrc is where Java source code files go, so classes placed elsewhere do not belong to the project's source.
Do this insteadPut every new class inside the src directory.
Common mistake Treating the JRE System Library as a place to write code
Why it failsIt holds the standard built-in classes that come with Java and is not something you write.
Do this insteadWrite your code in src and leave the JRE System Library as the Java standard classes.
Common mistake Expecting the project to appear before Finish is clicked
Why it failsThe project only appears in Package Explorer after Finish is clicked.
Do this insteadComplete the wizard and click Finish, then look for the project in Package Explorer.
Common mistake Confusing a view with a perspective and expecting one panel to change the whole window
Checking Your Understanding
Without looking back, predict what you will see in Package Explorer after you create a project named Greetings with the default settings and click Finish. Name the two items you expect to find inside the project and say what each one is for.
Hints
- Think about where your source files go.
- Think about which item holds the standard built-in classes that come with Java.
What do you think happens?
Which perspective is used for compiling and running programs, and which one helps find semantic errors?
Reveal answer
The Java Perspective is used for compiling and running programs. The Debug Perspective helps find semantic errors.
A perspective is the whole set of views in the window arranged for a job, so switching perspectives changes which views you see while you work.
Key Takeaways
- A workspace is the directory where your work is stored, and you choose one when Eclipse starts; several workspaces can exist within one installation.
- The Eclipse window is one large window divided into sections, and each section holds one or more views such as a text editor, an I/O area, or a project list.
- A perspective is the whole set of views arranged for a job: the Java Perspective is the default for compiling and running, and the Debug Perspective helps find semantic errors.
- To create a project, follow File, New, Java Project, enter a Project Name, set the execution environment to JavaSE 17 or later, and leave 'Create
module-info.javafile' unchecked for non-modular textbook programs. - A new project shows a src directory for your source code and a JRE System Library of standard built-in classes in Package Explorer.