How to configure Java 8 in Eclipse IDE

02 May 2014
By Gonçalo Marques
In this article we will configure Java 8 in Eclipse IDE.

Prerequisites

First you will need to download the following prerequisites (if you haven't already):

  1. Download the Java 8 JDK/JRE from Oracle download website
  2. Download Eclipse Kepler or later (in this article I have used Kepler 4.3.2 version with the required Java 8 patches already included)

After downloading both items above you may launch Eclipse IDE.

Configure Java 8 JRE in Eclipse

After launching Eclipse go through the following menu entries:

Window -> Preferences -> Java -> Installed JREs

Now click on Add... and set JRE home field with the path to your Java 8 JDK/JRE installation directory.

Configure Java 8 JRE in Eclipse

Configure Java 8 compiler in Eclipse

In order to configure the Java 8 compiler go through the following menu entries:

Window -> Preferences -> Java -> Compiler

Now change JDK compiler compliance level to 1.8

Configure Java 8 compiler in Eclipse

Note: We are assuming that you want to configure the Java 8 compiler in all projects. If you need to change the compiler only for a single project, right-click on the project and select Properties -> Java Compiler.

Testing

Now that everything is ready, we may create a sample project containing the following test class:

Testing a lambda expression

package com.byteslounge.test;

public class Test {

  public static void main(String args[]){
    int x = new Test().sum(1, 3, (int a, int b) -> a + b); 
    System.out.println("Result: " + x);
  }
  
  private int sum(int a, int b, TestInterface testInterface){
    return testInterface.sum(a, b);
  }
  
  private interface TestInterface {
    int sum(int a, int b);
  }
  
}

We are using a lambda expression in our class. Eclipse will successfully compile and run our test class, and produce the expected output:

Result: 4

Related Articles

Comments

About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile: https://github.com/gonmarques

He is also the author of the WiFi File Browser Android application: