Setting Up Your Eclipse Java Development Environment

So how do you setup your IDE (Integrated Development Environment) for java development? This is my usual setup in terms of how I like things to be done. This is how i usually setup my eclipse environment for java development.

Download Java


As of writing the current version of java 1.8 is 212. Please note that the Oracle java licensing has changed in April. For personal development, java is still free to download but after java 1.8 you must either use the openjdk version of java, or you can use Oracle java if you have purchased either support or an oracle production that includes java support in the license.https://www.oracle.com/technetwork/java/javase/downloads/index.html

Oracle Java 1.8 Download

Once you have signed up to oracle and downloaded java, run to install it in the usual place. It will usually end up in your ‘Program Files’ directory if you are installing on windows.

java install into program files

Download Eclipse

ok at this stage you are ready to go with java installed. Now you need to get your eclipse IDE. Eclipse is the open source IDE that many developers use for personal or commercial development alike. Developers vary on which IDE (Integrated Development Environment) they prefer to use, but my personal choice is Eclipse. Ive used it for a long time so im familiar with its quirks, and it gets the job done.

There are other IDEs available, with IntelliJ and Netbeans probably being the next most popular after Eclipse. But I suspect Eclipse is the most popular IDE for java development.

You can use Eclipse for Java development, but it can also be used for other languages/platforms too. I believe its also popular for C++ development, (which I dont do), but I do also use eclipse as my IDE for PHP development, that I have done a bit of recently.

https://www.eclipse.org/downloads/packages/

I usually download the “Eclipse IDE for Java Developers” from the above site, as that will give you what you need for getting started with java development, and you can add in other items later.

Once you have downloaded eclipse and installed it (there are various ways to install it and it usually has an installer with it), click on the eclipse executable to start if it hasnt started for you, and you should be presented with the welcome page the first time you open it. If you get asked about a workspace before this you can either accept the default or create a directory to store your workspace.

eclipse welcome page

Create A New Java Project

Lets fly straight into setting up a project as this will help us get things setup just the way we want it going forward. From the menu at the top click File…New…Java Project

file new java project

Once you click, you will be presented with the “Create A Java Project” screen.

Create A Java Project Eclipse

Give your project a name in the project name field, and then click on ‘Configure JREs’. This will allow us to point eclipse to the version of java that we just loaded.

You should see a screen similar to the above which allows you to chose which java runtime you want to use while you are developing and also for running your code from eclipse. I generally point it to the jdk version, as some plugins require a jdk to run, so it can make it easier to just use a jdk for everything just so you dont have to switch.

The only time this could be an issue is if you end up developing software that needs to run with just a JRE runtime of java, in that case make sure you test it against a JRE to ensure there are no dependencies on things that are only part of the JDK. But we can take that discussion offline (save for another time 🙂 )

Select You Java JRE / JDK

Click ‘Add’ on the right


JRE Type should default to Standard VM as above, so click next

Here will provide the location of the JRE/JDK. Click on the Directory button in the top right, then navigate to the jdk home of the java version you installed. For example C:\Program Files\Java\jdk1.8.0_212 as shown below.

Click ‘Select Folder’ button and this should update your JRE Definition page as shown.

Then click finish to continue with your java project setup. Tick the new version that we just installed and this should become our default JRE.

Click ‘Apply & Close’ and you should see the new version shown as the default.

Java Project Default JRE

Click next to go to the ‘build settings’ window. Here we want to setup our source folders. What I like to do is to have seperate test and main folders so that my test code will never be deployed onto my production environments, so its a good habit to get into.

Java Project Build Settings Page

Click on the ‘Create New Source Folder’ link. Type in ‘src/main/java’ then click finish.

Do the same to create ‘src/main/resources’, ‘src/test/java’ and ‘src/test/resources’.

Click finish and then your java project should be setup, with your newly downloaded JDK as the default.

First Java Project

Setup Maven In Eclipse To Manage Your Java Project Dependencies

Apache Maven is a project build and dependency management tool. Its mainly used in the java world to manage your projects dependencies on other libraries/frameworks that are held centrally in a repository such as the Maven Central Repository. With the use of a ‘Project Object Model’ xml file, more commonly known as the ‘POM’, it allows you to control how your project artifacts are built.

In simple terms it allows you to specify what versions of libraries you will use in your project, and how you code gets built. There are lots of other use cases also, but those are the most simple ones that most people are going to be aware of and need initially.

When you are first starting out and you are building simple ‘Hello World’ type apps, you really dont access many libraries outside of core java, so maven is not really needed. But once you start developing real apps and using functionality from various libraries, you will see that it very quickly becomes complex to manage the various dependencies, so a tool like Maven becomes essential to ensure a consistent environment for your code.

Adding Maven To Your Eclipse Project

Maven is generally bundled with java IDEs these days. To get maven setup for your eclipse project you need to create a build task that sets up your project and pulls in the required dependencies.

From the menu, click Run… Run Configurations… and then scroll down to Maven Build.

Right Click on Maven Build and click ‘New Configuration’ and you should see the screen below:

Change the name to something meaningful, like ‘Maven Build’ and then your project name. Click the workspace button to select the project you want built. In the goals, type in the following string:

eclipse:clean eclipse:eclipse -DdownloadSources -DdownloadJavadocs

This will clean up your project, apply the necessary changes to ensure your project is setup for maven and has the relevant dependencies in place as needed and setup the project classpath. It will also download the source and javadocs for those dependencies where its available. This is useful as something it can help figure out how to use a particular method on a library, especially when the documentation is poor.

Click Apply, then Run, and it should try and build your project, and you will see the output in the console as shown.

What we got was a build failure because we havent defined a POM xml file. So lets create one. Right click on the project and click New.. File..

Enter the file name as pom.xml and click finish

Double click the pom.xml file, and you should see the POM editor. Lets file in the group id, artifact id and version as shown below.

Click on the pom.xml tab in the editor and it will show you the resulting xml that has been generated.

We can run our maven build again by clicking on the ‘Run As…’ in the menu bar. This will run the last option we ran if we just click it, or we can select the drop down to select from recent runs.

Now the build ends successfully as the have a POM, and its ready to setup dependencies.

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< com.mytest:firstproject >-----------------------
[INFO] Building firstproject 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-eclipse-plugin:2.10:clean (default-cli) @ firstproject ---
[INFO] Deleting file: .project
[INFO] Deleting file: .classpath
[INFO] Deleting file: .wtpmodules
[INFO] Deleting file: .settings
[INFO] 
[INFO] >>> maven-eclipse-plugin:2.10:eclipse (default-cli) > generate-resources @ firstproject >>>
[INFO] 
[INFO] <<< maven-eclipse-plugin:2.10:eclipse (default-cli) < generate-resources @ firstproject <<<
[INFO] 
[INFO] 
[INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ firstproject ---
[INFO] Using Eclipse Workspace: C:\\workspace
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "firstproject" to C:\workspace\firstproject.
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.228 s
[INFO] Finished at: 2019-06-24T21:45:21+01:00
[INFO] ------------------------------------------------------------------------

Testing Maven Setting Up Dependencies

Lets confirm that maven will sort out the relevant dependencies when we add them to the POM. We will start by adding a simple junit test and see if we can run it.

package com.mytest;

import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class HelloWorldTest {

    @BeforeClass
    public static void beforeClass() {
	System.out.println("Runs Before Class");
    }

    @Before
    public void before() {
	System.out.println("Runs Before Test Case");
    }

    @Test
    public void isGreaterTest() {
	String helloWorld = "Hello World";
	assertTrue("Hello World", helloWorld.is("Hello World!"));
    }

    @After
    public void after() {
	System.out.println("Runs After Test Case");
    }

    @AfterClass
    public static void afterClass() {
	System.out.println("Runs After Class");
    }
}

Ive added a very simple junit test java class called HelloWorldTest. Eclipse is showing errors because it doesnt recognise the annotations such as @BeforeClass, as we dont have the junit dependencies on the project, so lets add them by editing the POM.

Lets go to the maven central repository and search for junit

If we click on Junit, we can see the latest stable release is 4.12.

If you click on the version, it will show you more details and the dependency that needs to be added to the POM.

We can either type these details into the editor, or we can click on the dependency details and add it directly to our project POM. Lets see how to do it using the editors dependencies tab. If we click on the dependencies tab in our POM editor we see the below:

Eclipse Pom Editor

If you click the ‘Add…’ button next to the dependencies, on the next page we see an error, ‘Index downloads are disabled, search results may be incomplete.’. We can resolve this by switching on the Maven Index downloads, but ive found this is buggy and never completes so ignore this error.

Add the details of the junit dependency to the editor as below:

junit junit 4.12 test

Click ‘ok’, then if you click on the pom.xml tab, you can see that the junit dependency has been added.

Now lets save the pom.xml changes, and do the maven rebuild again.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.277 s
[INFO] Finished at: 2019-06-24T22:25:40+01:00
[INFO] ------------------------------------------------------------------------

We get build success for that, and our project looks slightly different as it now has junit and hamcrest as reference libraries.

Modifying the pom slightly to include more hamcrest dependencies… Also i tend to use properties to store the version numbers so that they can be changed easily, plus also sometimes multiple jars within a library share version numbers.

So with our HelloWorldTest now as follows with no errors or warnings

Lets right click our isTest method and run as a junit test.

And we get the result

if we modify our isTest method from:

To


We now get green for successful junit execution. So we are setup and ready to go and our code is compiling and executing.

Leave a Comment

Your email address will not be published. Required fields are marked *