Create and manage a Java project

Posted on in Blog

For this first post, I will start slowly with the creation and configuration of a brand new Java project. Then we will have a look at the various option to set up our project from the configuration of the JVM to the different source folders that we want to use. We will see how we can organize our Java projects and after that we will move on to the creation of our first packages and classes. Finally we will conclude with the project dependencies. Dozens of screenshots are also available in order to illustrate the information provided in this blog post, don’t forget to click the links.

1.1 - New project wizard

image

In order to start working on a new Java application, you need to create a new Java project. For that, you have access to the new “Java Project” wizard from the “File” menu. In the first page of the wizard used to create the new Java project, you have to choose the name of the project. You also have the ability to select the location where the project will be created. This features gives you the ability to create a project outside of your Eclipse workspace.

This is particularly useful to let you create your project in a Git repository located somewhere else on your file system. You can then import easily the same project in various Eclipse workspaces and work from those different workspaces without having to synchronize later on different versions of your application. The workspaces will be kept almost empty, except for a folder “.metadata” which contains the settings of the workspace. You can easily setup a new Eclipse with a brand new workspace and resume your work. I would go as far as saying that except for prototyping code, most projects should be created outside of your workspaces which can, that way, stay disposable.

In this page, you can also select the Java Runtime Environment (JRE) of your project. You have the choice between using a selected execution environment JRE, using a project specific JRE or using the default JRE. I will detail the configuration of the JRE later but I recommend using an execution environment JRE. You can also choose the layout of your project from this page (we will have a look at this later) and you can choose to add your project to a working set (also, more on that later). On the second page of this wizard you can configure several options of you Java project, those options being accessible after the creation of the project, we will explore them later.

1.2 - Filtering the content of the Package Explorer view

The first view you are manipulating while working on a new Java project is the “Package Explorer” view since it contains your newly created Java project. This view contains lots of options and we will start by having a look at filtering. In the Package Explorer view, you can hide various kind of artifacts in order to help you get more focused on your work. By default, all “.*” resources are hidden. I can change this settings to reveal them in the “Package Explorer” view.

1.3 - Hidden resources: “.project”, “.settings” and “.classpath”

By using the filtering to show all the “.*” resources, three new resources have appeared inside of our Java project:

the file “.project” the file “.classpath” the folder “.settings”

The file “.project” is used by Eclipse in order to store some metadata about the nature of the project, its name and the tools that should be used with this project. If we open it, we can see that inside this XML file, the name of the project and the natures of the project. Eclipse will use the name inside this file “.project” to display our project in the Package Explorer view. It does not have to be the name of the root folder of the project. By selecting a custom location for the creation of the project you can make the name of the root folder of the project different from the name of your project. The nature is used by Eclipse to known the kind of the project that is manipulated. In our case, we only have the nature “org.eclipse.jdt.core.javanature” since we have a basic Java project. You can also see the “build specification” of the project. This information is used by Eclipse to known which tools should be called when the project is modified. In our case, the builder with the id “org.eclipse.jdt.core.javabuilder” will be used when the project is modified. The Java builder is responsible of the compilation of the Java source code.

In Eclipse, you have access to several levels of settings, Eclipse-based settings and project-based settings. The folder “.settings” located in your Java project contains all the project-specific settings of the Eclipse tools that you are using. By default, the JDT setup some default options in the setting of your project. We will have a detailed look at the various Eclipse and JDT settings in the third post. The Eclipse-based settings are located in the “.metadata” folder of your workspace.

The file “.classpath” is used by Eclipse to compute the classpath of your project (what a surprise!). In this file, you can find the list of your source folders with their output folders and also all your dependencies. You can also find in this files the various container used in your project. Those container are Eclipse metadata that are resolved by the tooling to various artifacts. By selecting an “execution environment JRE” while creating our project, we essentially told Eclipse that we are using the container for “JavaSE-1.7”, we don’t care where it’s located in the file system, we don’t care about the precise version of Java 7, we just want this container to be resolved against a JRE known by Eclipse that matches “JavaSE-1.7”. This way we can share our project on a repository without having the absolute path of our JRE in the settings of our project. Other team member will just need to have a JRE compatible with “JavaSE-1.7” configured in their Eclipse (more on that later).

1.4 - Projects and working sets

image

If you are working on an existing Java application, one day or another, your workspace will be cluttered by countless of projects. In order to categorize properly your workspace, the Package Explorer view let you define “Working Sets”. You can create a new “Working Set” and then drag and drop you project inside this way you can group your project by concerns inside of your workspace. Keep in mind that “Working Sets” are a logical structure and as such you can have a project in multiple “Working Sets”. You can later drag and drop any resources in the resource set of your choice.

1.5 - Go into

Now that you have categorize the thousands of projects that may exists in your workspace, you may still have problems to easily manipulate your code if one of your “Working Set” contains dozens of projects. Sure you can separate those projects in smaller “Working Sets” but what if it’s not relevant for your need or what if you want to work on a project containing hundreds of packages? The Package Explorer view let you “go into” a container to remove all the useless content from your sight. This way, you can work only on a given package or folder if you need it without having to see the rest of your artifacts. You can also easily move to an upper container.

1.6 - Java Runtime Environments

image

You can configure in Eclipse the various JRE that can be used by your Java projects in the section “Installed JREs” of the preferences of your Eclipse. You may need to press “OK” to fully validate the changes done to the list of installed JREs. Once applied, you can go back to configure your “Execution Environments”. There, you can match a given execution environment to a specific “Java Runtime Environment”. This way, when the container for “JavaSE-1.7” of your new Java project needs to be resolved, it can be linked to a specific JRE. You can then go in the properties of the build path of your project and in the “Libraries” section, you can edit the JRE selected. The settings is also accessible on the new Java project wizard as we have seen it before.

1.7 - Source folders

In most of the Java project, the Java source code is located in a folder named “src/” under the root of the project. You can define other source folders if you want. A good practice, for example, is to put the generated code in packages located in the “src-gen/” source folder. This way, if you need to clean the generated code, you can delete easily this code. Maven developers tends to use “/src/main/java/” as their source folder. Whatever your reason, Eclipse let you create easily new source folders, you can even convert existing folders into source folders or remove source folders without removing the physical folders. All those changes are stored in the file “.classpath” that we saw at the beginning.

1.8 - Using libraries

image

While you are working on your Java project, a time will come when you will have to use external libraries. There are several ways to resolve a link to an external dependency. The Maven tooling for example creates its own container in the classpath of your Java project and then it uses a dedicated builder to analyze the file pom.xml located in the project to determine the dependencies that should be downloaded and linked with the project. Sometimes, you don’t have the choice and you need to depend on a good old jar file. In order to do that, you can create a “lib/” folder at the root of the Java project where you can put you jar files. In a couple of clicks you can add those libraries to your classpath. You could also add the external jars from “Libraries” section of the “Java Build Path” settings.

1.9 - Packages, Classes, Interfaces and enumerations

Now, your projects are configured with their “Java Runtime Environment”, their libraries and they are properly organized within Working Sets. It’s about time to start building this application. We have seen how you can organize your source folders. With the JDT, you can easily create packages with a dedicated action in the toolbar of the Java perspective. In the same way you can create Classes, Interfaces or Enumerations. You can also create those artifacts from the New menu in the contextual menu or in the File menu.

1.10 - Package presentations

You can also select how you want to display packages in the “Package Explorer” view. You can keep the flat representation which is used by default or you can switch to a hierarchical view.

1.11 - Link with the editor

When you are navigating in a big Java application, you will end up with some code in your editor coming from an unknown place. You could have open a class of one of your dependencies for example. In order to find out where the code displayed in you editor is, you can click the button “Link with the editor” in the “Package Explorer” view. The resource containing the code displayed in the active editor will be selected in the “Package Explorer”. If you keep navigating in the code, the selection will be updated. It will be very useful in the next post.

1.12 - Open in new window

image

Those of you who possess a dual-screen configuration may want to extend your Eclipse across both monitors. Eclipse let you open a project in a brand new window.

1.13 - Project dependencies

If you want to properly maintain your project, you will have to split it in several Java project as it grows. Eclipse let you easily setup projects dependencies in a couple of clicks.

1.14 - Close a project

The more opened projects you have in your workspace, the slower Eclipse will be. To speed up Eclipse, close useless projects!

1.15 - Delete a project

Of course you can also delete projects from Eclipse, but be careful, the default behavior remove the project from the workspace but it does not delete the project from the file system. If you have created your project in a custom location like a Git repository, this may be the behavior that you desire. If you really want to delete completely the project from the file system, do not forget to check the matching option.

Regular Java developers should not have learned anything really new in this post, this one was mainly for newcomers. In the next part, we will get our hands dirty as we will start coding. You’ll see the tips and tricks of the JDT that I’m using everyday to speed up massively my Java development.

Auteur d'origine: sbegaudeau