How to configure Java 8 in Eclipse IDE
Prerequisites
First you will need to download the following prerequisites (if you haven't already):
- Download the Java 8 JDK/JRE from Oracle download website
- 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 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
Testing
Now that everything is ready, we may create a sample project containing the following test class:
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: