Concepts / Running a Java Program in Eclipse

Running a Java Program 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

Why Code Starts with a Project

Before you can write a single line of Java in Eclipse, you have to decide where that code will live. This article follows one path: opening the wizard that builds a new Java project, deciding what to fill in, and then examining what Eclipse puts on your screen. Read it with a real Eclipse window open so you can check each step as you go.

What do you think happens?

Can you start writing Java in Eclipse by simply opening a loose source file with no project around it?

  • Yes, a loose source file is a valid starting point

  • No, all work is organized inside a project

Reveal answer
Answer:

No, all work is organized inside a project

Eclipse does not treat a loose source file as a starting point. All of your work is organized inside a container called a project.

What a Project Gathers Together

Definition

A project is the container in Eclipse that gathers together the source files, the library settings, and the other resources that a program needs. Because of this grouping, the IDE always knows which files belong to which program.

Think of the project as the single place where one program lives. Your source files, the settings that say which Java environment the program runs against, and any other resources are all held together in that one container. When you later look at a project, you are looking at everything Eclipse needs to know about that program.

Opening the New Java Project Wizard

  1. 1
    Click the File menu in Eclipse.
  2. 2
    Inside File, open the New submenu.
  3. 3
    Choose the Java Project command.
  4. 4
    A window opens in which you describe the project before Eclipse creates it.

At this stage nothing has been built yet. You are only supplying the choices that Eclipse will use. The project does not appear in the Package Explorer while this window is still open; it appears only after Finish is clicked.

Filling In the Wizard Settings

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. Beyond the name, the wizard has two sections 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. The table below summarizes the settings this course expects.

Wizard settingWhat it controlsSetting this course expects
Project NameThe name you see in the project listAny name that identifies the program
JRE (execution environment)Which Java environment the project runs againstJavaSE 17 or later
Create module-info.java fileWhether the project is treated as a modular programUnchecked for non-modular textbook programs

The three wizard settings a learner must check on each new project

Why the Execution Environment Should Be JavaSE 17 or Later

The JRE setting in the wizard should use an execution environment of JavaSE 17 or later. The JRE choice you make in the wizard is the part of the project that shows up as the JRE System Library, which holds the standard built-in classes that come with Java. In other words, the execution environment you pick determines which set of built-in classes your program is compiled and run against.

The source for this course asks for JavaSE 17 or later as the expected setting, so when you create a project, look at the JRE field and confirm that the version chosen is 17 or newer. If an older version is selected, change it before clicking Finish so that the project's library matches the course's expectation.

When to Uncheck module-info.java

The second setting that needs attention is the 'Create module-info.java file' option. The course expects this option to be unchecked for non-modular textbook programs. Unchecking it tells Eclipse that the project is not a modular program. The diagram below shows the decision you face at this point in the wizard.

New Java ProjectwindowCreatemodule-info.javafile?Leave checkedproject treated as modularClick FinishUnchecknon-modular textbookprogramfill in settingskeep optionuncheck optiontextbook path
Which path should I take at the new-project dialog: keep module-info.java or uncheck it, and what changes in the project as a result?

Reading the Package Explorer

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. Click the small triangle or plus sign beside its name to expand it and see what it contains. Assuming the default settings, two items matter most: the src directory and the JRE System Library.

Project Nameyour Java projectsrcsource code files go hereYour classesadded inside srcJRE System Librarystandard built-in classescontainscontainsholds
What does the project contain, and which items are nested inside which: src, your classes, and the JRE System Library?

The src directory is where your Java source code files go, and 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.

What do you think happens?

You have just clicked Finish on a new Java project. Which two items should you look for first in the Package Explorer?

  • src and the JRE System Library

  • module-info.java and the Package Explorer

Reveal answer
Answer:

src and the JRE System Library

With the default settings, the src directory and the JRE System Library are the two items that matter most in a newly created project.

Worked Setup of a Textbook Project

Worked example

Creating a non-modular textbook project

Create a new Java project for a textbook program named TextbookPractice, using the course's expected settings, and confirm what appears in the Package Explorer.

  1. 1
    Open the wizard

    Click File, then New, then Java Project.

  2. 2
    Name the project

    Type TextbookPractice in the Project Name field. Any name is allowed, so choose one that identifies the program.

  3. 3
    Check the JRE setting

    Confirm that the execution environment is JavaSE 17 or later. Do not assume the value carried over from a previous project.

  4. 4
    Uncheck module-info.java

    Because this is a non-modular textbook program, uncheck the Create module-info.java file option.

  5. 5
    Finish and inspect

    Click Finish, then expand TextbookPractice in the Package Explorer. Look for the src directory and the JRE System Library.

Result

A project named TextbookPractice appears in the Package Explorer, containing a src directory for source files and a JRE System Library of standard built-in classes.

Mistakes Beginners Make with Project Setup

  • Common mistake

    Trying to write Java without creating a project first

    Why it fails

    Eclipse does not treat a loose source file as a starting point, so the code would not be organized inside a project.

    Do this instead

    Create a Java project through File, New, Java Project before adding any classes.

  • Common mistake

    Assuming the wizard settings carry over unchanged

    Why it fails

    Eclipse remembers the JRE and Module settings, which means earlier choices can persist into a new project.

    Do this instead

    Check the JRE and Module settings again for each new project.

  • Common mistake

    Leaving the JRE set to an older execution environment

    Why it fails

    The JRE setting controls the JRE System Library, so the course's expected JavaSE 17 or later setting is not met.

    Do this instead

    Set the execution environment to JavaSE 17 or later before clicking Finish.

  • Common mistake

    Leaving module-info.java checked for a textbook program

    Why it fails

    The course expects this option to be unchecked for non-modular textbook programs.

    Do this instead

    Uncheck the Create module-info.java file option for non-modular textbook programs.

  • Common mistake

    Looking for the project before clicking Finish

    Why it fails

    The project only appears in the Package Explorer after Finish is clicked, not while the window is still open.

    Do this instead

    Finish the wizard first, then look for the project in the Package Explorer.

  • Common mistake

    Placing class files outside src

    Why it fails

    The src directory is where your Java source code files go, and classes belong inside it.

    Do this instead

    Add new classes inside the src directory.

Practice: Build a Project from Scratch

EASY

Create a new Java project in Eclipse using File, New, Java Project. Choose a Project Name that identifies a program of your own, confirm that the JRE is set to JavaSE 17 or later, and uncheck the Create module-info.java file option for this non-modular textbook program. After clicking Finish, expand the project in the Package Explorer and name the two items that matter most.

Hints
  • Start from the File menu and open the New submenu before choosing Java Project.
  • Remember that the project only appears in the Package Explorer after Finish is clicked.
  • The two items to name are the src directory and the JRE System Library.

Key Takeaways

  • A Java project in Eclipse is created from File, then New, then Java Project, and its Project Name can be anything that identifies the program.
  • The course expects the JRE execution environment to be JavaSE 17 or later, because the JRE choice controls the JRE System Library of standard built-in classes.
  • Leave the 'Create module-info.java file' option unchecked for non-modular textbook programs, and recheck the JRE and Module settings for every new project since Eclipse remembers them.
  • In the Package Explorer, the src directory holds your source code files, while the JRE System Library is provided by Java and is not something you write.