Concepts / Writing Your First Java Class in Eclipse

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.

  • Programming

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?

  • Yes, it appears as soon as you type a name

  • No, it appears only after Finish is clicked

Reveal answer
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.

  1. 1
    Open the File menu in Eclipse
  2. 2
    Open the New submenu
  3. 3
    Choose the Java Project command
  4. 4
    Fill in the project settings in the wizard window
  5. 5
    Click 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.

StageWhat Eclipse holdsVisible in Package Explorer?
Wizard window openYour chosen settings onlyNo
After Finish is clickedA project with a src directory and a JRE System LibraryYes

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.

Java projectExecution environmentJavaSE 17 or laterJRE System LibraryStandard built-inclassesset in wizardcontrolscontains
How does choosing JavaSE 17 or later as the execution environment connect the project to a specific JRE library?

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.

Non-modulartextbook program?Uncheck the optionClick FinishKeep the optioncheckedyesno
Under what conditions should I uncheck 'Create module-info.java file', and what changes in the project as a result?

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.

Project namesrcSource code directoryYour classesAdded inside srcJRE System LibraryStandard built-in classescontainscontainsholds
What does a newly created Java project contain, and how are the src folder and the JRE System Library nested within it?

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.

Worked example

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.

  1. 1
    Locate the project

    Find Greetings in the Package Explorer on the left side of the Eclipse window. It appears only because Finish was clicked.

  2. 2
    Expand the project

    Click the triangle or plus sign beside Greetings to reveal its contents.

  3. 3
    Find src

    src is the source code directory. This is where your Java files belong.

  4. 4
    Find 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.

  5. 5
    Place the first class

    Any class you add later goes inside src, not directly in the project's other entries.

Result

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 like

    Typing a name and looking in Package Explorer before clicking Finish

    Why it fails

    The window only describes the project. Nothing is built until Finish is clicked.

    Do this instead

    Click Finish, then look for the project in Package Explorer.

  • Common mistake

    Assuming the JRE and Module settings carry over correctly

    Why it fails

    Eclipse remembers these settings from earlier, so a previous choice may not suit the new project.

    Do this instead

    Check 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 fails

    The source says this option should be unchecked for non-modular textbook programs.

    Do this instead

    Uncheck 'Create module-info.java file' for this kind of program.

  • Common mistake

    Writing code directly into the JRE System Library

    Why it fails

    The JRE System Library holds standard built-in classes that come with Java. It is not your code area.

    Do this instead

    Put your class files inside the src directory.

Creating a Project From Scratch

EASY

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.java file 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.