1.3 Eclipse

Prior to writing more code, we’ll download and start using something called Eclipse—an integrated development environment. Eclipse simplifies a whole lot of tasks, the best ones being that it automatically compiles and runs our code for us.

Installation

Eclipse can be downloaded by using this link, which will download an installer. By running the installer, a new window is going to pop up after an initialization screen. You’ve successfully downloaded and run the Eclipse installer! Now all that’s left is to install the correct version of Eclipse and put it at a desired location.

We’re going to want to choose to download the “Eclipse IDE for Java Developers”, which should be the first alternative inside the installer’s list—listing all the different versions of Eclipse you can choose to download. Had you for instance wanted an IDE for C and C++, you would have went with the “Eclipse IDE for C/C++ Developers”. Since this tome is on Java, go ahead and choose the Java one.

Once you’ve chosen the Java version, first decide whether you’d like Eclipse at the default location or choose a new one by entering a new install path. Follow that by pressing the huge “Install” button at the bottom of the window. The installation is going to take a few seconds, but once installed you’re good to go. It’s time to get started using Eclipse!

Press the big “Launch” button or locate Eclipse inside your applications.

Workspace

Whenever launching Eclipse—whether for the first time ever or not—you’ll be presented with the opportunity to choose the location for your workspace. This is a folder where all your future Java projects will be stored. Stick to the default folder or enter in a new path, it matters not, decide on whether you’d like it to be your default workspace or if you’d like to be prompted every time you launch Eclipse. Press “Ok” and Eclipse will open up.

Hello, Eclipse!

When you first launch into a new workspace, you will be welcomed by a heartwarming little welcome screen—be a meanie and close that down. Before you lies the sight of the beautifully laid out Eclipse IDE. On the left-hand side of the screen you’ll see something called “Package Explorer”—this is where you’re able to view all your Eclipse projects. Let’s create a new one!

New project

Press File > New > Java Project—you will be presented with the opportunity to choose some basic information regarding your new project. Go over your mind and all of its thoughts and choose a fitting name for your soon to be created Java project—I’ll go with “Sandwich”, since I’m a big fan of them. It’s worth to notice that all project names should be written using camel case by convention—at the same time it makes you project names look cool! Hit “Finish” and you’ll notice that our new project popped up in the package explorer.

Expand the project in the package explorer by pressing the little down arrow to the left of the project’s name. Inside our project folder—the one we expanded—there’s an “src” folder. The “src” folder is our project’s source folder. This is where we put all of our .java files. Let’s create our first Java source file inside of Eclipse—this is going to be a Java class. As to why it’s called a class, we’ll go over in a later section.

Package and class

There’s however one more thing we’d like to do prior to creating a class—creating a package! Although there’s no real point in using packages at this point, since our project almost always is going to consist of one file, we’ll do it by convention.

Packages in Java are used to give developers the opportunity to use a somewhat unique structure for their projects. Go ahead and right click your source folder, choose New > Package and press down. You’ll notice a new window popping up, there to help you create your package. Whilst the package name in theory can be any gobbledygook you choose to input, by convention it should resemble your website backwards seperated by periods followed by the project’s name. For that reason, I’ll choose to input “com.junivörs.sandwich”. Hit enter, and a new package will pop up inside your source folder!

Back to the creation of a class! Right click your newly created package, choose New > Class and press down. Now it’s about time we choose a good name for our class, which also by convention is written using camel case—just like the name for our project. The class name can be anything you’d like, although it’s probably good to name your class containing an entry point into your program—the main method—something like “Start” or “Core”. I feel like being a rebel, so I’ll go with “Sausage” for my class’ name. And as if by flying piñatas, we’ve acquired ourselves a class!

Your class will automatically be shown in the editor:

package com.junivörs.sandwich;

public class Sausage {

}

Hey! Somebody moved up my {! It’s totally fine to start a code block from the same line as the thing it’s attached to—there are however good reasons as to why more experienced developers choose to abandon this practice after a while. I’m just going to move that down a line, because otherwise I’m going to go rather crazy!

The package on top of the code tells Java what package this class belongs to—as you can clearly see, it belongs to whatever you chose to name your package. Let it be there and don’t worry about it, we’ll get into packages later.

Let’s add in our main method as well as a welcoming message to the world of Eclipse.

Cod’n

Enter the things from the previous sections into your code:

package com.junivörs.sandwich;

public class Sausage
{
    public static void main(String[] args)
    {
        System.out.println("Hello, Eclipse!");
    }
}

It now looks awfully similar to our previous sections—on top of the screen there’s a green run button, to the right of the little insect icon, press it! Whether or not you’ve saved manually over the whole coding experience, you’ll either be presented with a “Save and Launch” window or not, respectively. It’s good to save manually whenever you change something since should Eclipse decide to die on you at any point you’ll still have it saved on your computer. Press “Ok” on the “Save and Launch” window if it popped up—your code will have launched either way!

At the bottom of the screen, there’s a small console window which should say:

Hello, Eclipse!

We recognize the feature of printing, don’t we?

Moreover

This is how we’ll be compiling and running our projects from now on. Eclipse clearly simplifies the whole coding experience—especially when your project starts getting large!