Writing Your First Java Class in Eclipse
A Java project in Eclipse is created from File, then New, then Java Project, where you choose a Project Name that can be anything.
Where Your First Java Code Lives
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. Instead, all of your work is organized inside a container called a project. A project gathers together the source files, the library settings, and the other resources that a program needs, so the IDE always knows what belongs to which program.
What do you think happens?
When the New Java Project window is still open and you are filling in its fields, do you expect the new project to already appear in the Package Explorer?
Reveal answer
No, it appears only after Finish is clicked
The window is only describing the project. Nothing is built at this stage; Eclipse uses your choices to create the project when you click Finish.
Starting From the File Menu
This article follows one path: opening the wizard that builds a new Java project, deciding what to fill in, and then examining what Eclipse actually puts on your screen. The starting point is the File menu. Inside it, open the New submenu, then choose the Java Project command. This opens a window in which you describe the project before Eclipse creates it.
- 1Open the File menu in Eclipse
- 2Open the New submenu
- 3Choose the Java Project command
- 4Fill in the project settings in the wizard window
- 5Click Finish to have Eclipse create the project
Read this sequence with a real Eclipse window open so that each step can be checked as you go. The wizard is a planning stage. Nothing is built yet while the window is open; you are only supplying the choices that Eclipse will use later.
Tracing What Exists at Each Stage
The easiest way to understand the wizard is to ask, at each moment, what Eclipse actually holds. Before Finish, Eclipse holds only your answers. After Finish, it holds a real project with its own contents. The table below traces that change.
| Stage | What Eclipse holds | Visible in Package Explorer? |
|---|---|---|
| Wizard window open | Your chosen settings only | No |
| After Finish is clicked | A project with a src directory and a JRE System Library | Yes |
State of a new Java project at two stages of creation
Naming the Project
The first field you must fill in is the Project Name. Eclipse is flexible here: the name can be anything you like. Choose something that identifies the program, because that name is what you will see in the project list later.
Choosing the JRE Execution Environment
The wizard has two sections beyond the name that need your attention. One controls which Java environment the project runs against. For this course, the execution environment should be set to JavaSE 17 or later. This setting is the JRE choice, and it matters because it determines which JRE the project is connected to.
The diagram shows the chain. You choose the execution environment in the wizard, and that choice is what the JRE System Library reflects inside the project. You will see the library itself later, when you examine the new project's contents.
Deciding on the module-info.java Option
The other section of the wizard controls whether the project is treated as a modular program. It contains the option labeled 'Create module-info.java file'. For non-modular textbook programs, which is the case this course expects, that option should be unchecked. The decision is a simple one: find out whether the program is modular, then set the option to match.
The source states the unchecked case explicitly for non-modular textbook programs. The other branch in the diagram simply reflects the wizard's purpose: the option controls modular treatment, so a program that is not being written as a modular program should not have it switched on for this course.
Inside the New Project
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 that you can expand. Click the small triangle or plus sign beside its name to see what it contains. Assuming the default settings, two items matter most at this point.
The src directory is where your Java source code files go. When you later add a class, it belongs inside src. The JRE System Library is different: it is not something you write. It is the collection of standard built-in classes that come with Java, and it is the part of the project that the JRE setting controls. This is why the JRE choice made in the wizard shows up in the project's contents.
Reading a new project after creation
You have just created a project named Greetings with the execution environment set to JavaSE 17 or later and the module-info.java option unchecked. Describe what you should see in Package Explorer and where your first class should go.
- 1Locate the project
Find Greetings in the Package Explorer on the left side of the Eclipse window. It appears only because Finish was clicked.
- 2Expand the project
Click the triangle or plus sign beside Greetings to reveal its contents.
- 3Find src
src is the source code directory. This is where your Java files belong.
- 4Find the JRE System Library
This library holds the standard built-in classes. You do not write code here; it reflects the JRE setting you chose.
- 5Place the first class
Any class you add later goes inside src, not directly in the project's other entries.
Greetings contains a src directory for your code and a JRE System Library of standard built-in classes. Your first class will live inside src.
Mistakes Beginners Make With New Projects
Common mistake Expecting the project to exist while the wizard is still open
What it looks likeTyping a name and looking in Package Explorer before clicking Finish
Why it failsThe window only describes the project. Nothing is built until Finish is clicked.
Do this insteadClick Finish, then look for the project in Package Explorer.
Common mistake Assuming the JRE and Module settings carry over correctly
Why it failsEclipse remembers these settings from earlier, so a previous choice may not suit the new project.
Do this insteadCheck the JRE and Module settings again for each new project.
Common mistake Leaving the module-info.java option checked for a non-modular textbook program
Why it failsThe source says this option should be unchecked for non-modular textbook programs.
Do this insteadUncheck 'Create module-info.java file' for this kind of program.
Common mistake Writing code directly into the JRE System Library
Why it failsThe JRE System Library holds standard built-in classes that come with Java. It is not your code area.
Do this insteadPut your class files inside the src directory.
Creating a Project From Scratch
Create a new Java project in Eclipse using the File, New, Java Project path. Give it any name that identifies the program, set the execution environment to JavaSE 17 or later, and uncheck the module-info.java option for a non-modular textbook program. After clicking Finish, expand the project in Package Explorer and identify the src directory and the JRE System Library.
Hints
- Start from the File menu, then open the New submenu and choose Java Project.
- Remember that the project appears only after Finish is clicked.
- src holds your source code; the JRE System Library holds standard built-in classes.
Key Takeaways
- A Java project in Eclipse is created from File, then New, then Java Project, and the Project Name can be anything that identifies the program.
- Nothing is built while the wizard window is open; the project appears in Package Explorer only after Finish is clicked.
- The execution environment should be set to JavaSE 17 or later, and the Create
module-info.javafile option should be unchecked for non-modular textbook programs. - A new project contains a src directory for your source code and a JRE System Library of standard built-in classes, which the JRE setting controls.