Java Workspace & First app Tutorial

In this Java tutorial we set up a workspace to develop Java apps.

We discuss the concept of a workspace and how to create a new Java project in Eclipse.

Finally, we write our first simple Java app that prints a message to the console.

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

Development Workspace

Before we write our first Java app, we need to create a workspace, or at least a project.

  • Projects are the individual applications we want to create. For example, if we wanted to create a Media Player and a Calculater app, they would be separate projects.
  • A workspace is where all our projects are stored, as well as some of the IDE’s settings.

If you’re not following along with Eclipse, you may have to follow a different process to create a workspace or project in your IDE.

Create a project in your IDE and call it ‘LearningJava’.

If you are following along with Eclipse, please see the two sections below on how to setup a workspace, and how to create a new project.

Otherwise, go to the First App Section of this lesson.

How to setup a workspace in Eclipse

When we run Eclipse, the launcher will pop up and ask us to select a directory for our workspace.

A workspace in Eclipse is a directory to store all our separate projects.

Select a workspace in Eclipse

We chose a folder on our C:\ drive called ‘EclipseProjects’.

We also chose to use this workspace as our default, and not ask again. If this checkbox is not selected, the launcher will ask you to select a directory every time you run Eclipse.

If Eclipse doesn’t show the launcher, don’t worry, you can set the workspace by going to File > Switch Workspace > Other.

How to create a new Java Project in Eclipse

Projects are the individual applications we want to create.

For example, if we wanted to create a Media Player and a Calculater app, they would be separate projects, but still be stored in the same workspace.

To create a new project in Eclipse, we choose File > New > Java Project.

Create a new Java Project in Eclipse

Choose the same parameters for your project.

  • Project name: LearningJava
  • Use default location: Checked
  • Use an execution environment JRE: JavaSE-13
  • Use project folder as root for sources and class files: Selected
  • Add project to working sets: Unchecked In a production environment we would typically separate source and class files, as well as modify some project specific settings.

Click Finish and wait for the project to load in Eclipse.

If you only see the Welcome page, find and click on the Restore button in the top left corner, or simply close the Welcome page.

Restore project

First Java App

We’re finally ready to start programming. We’ll write a simple application that prints a message to the console.

If you’re not using Eclipse:

  1. Create a new class file called ‘Program.java’.
  2. Copy and paste the code below into the file.
  3. Compile/Run the file.

If you are using Eclipse:

  1. Go to File > New > Class.
  2. In the New Java Class window, specify the Name as ‘Program’.
  3. Click Finish.
  4. Copy and paste the code below into the file.
  5. To compile and run the application, go to Run > Run or click on the big round green ‘play’ button on the taskbar.
Example:
public class Program {

    public static void main(String[] args) {

        System.out.println("Hello, World!");
    }
}

In the terminal/console window you should see the following output.

Output:
 Hello, World!

Congratulations, you’ve just written, compiled and ran your first Java application.