How to enable lambda expressions in Eclipse Java

This page will show you a step by step tutorial on how to enable lambda expressions for Java in Eclipse.  You may have found this page if you’ve received an error similar to the one shown below.

Lambda expressions are allowed only at source level 1.8 or above
Lambda expressions are allowed only at source level 1.8 or above

The reason this is occurring is due to the fact that Eclipse will sometimes choose a default JRE version for your projects that is an earlier version than the latest release.  There are benefits to writing code that is compatible with previous versions, such as supporting a wider potential user base, but in most cases it is fine to update to the latest version.  In this case, it is the only way to enable lamda expressions for Java.

First you’ll want to right-click your project name in the Package Explorer and select Properties.

Eclipse Package Explorer

Next select Java Build Path from the left list of options and then navigate to the Libraries tab.

Eclipse Java Libraries

Select JRE System Library and click Edit…

Eclipse JRE System Library

Select the Execution Environment drop-down list and choose JavaSE-1.8.  Or, if you have a more recent version of Java than 1,8, feel free to choose that.

Eclipse Execution Environment

And you’re finished!  You should no longer receive errors for attempting to use lambda expressions in your program!  If you are still experiencing issues, restarting Eclipse may remove any residual erroneous error messages.

If you found this tutorial helpful you might enjoy my YouTube channel!

jfx1

JavaFX Tutorial – Make a Window

Make a Program – JavaFX Tutorial: Make a Window

 

For instructions on setting up Eclipse (our IDE), click here.

Start by making a new Java project in Eclipse by using File -> New -> Java Project.  After that, create a new class named Main and open it.  Create the entry point to our program as such:

Next we need to declare two variables, a BorderPane and a Scene:

In order to access these class types, we’ll need to make our Main class extend Application, which is a JavaFX interface.  We should now have a class like this:

Next we can import the Application, BorderPane and Scene resources by hovering over them and selecting the import option, or by hitting Ctrl+Shift+O.  If you are unable to import these classes, it’s possible that your version of Java requires a custom access modifier in order to use JavaFX.  Please check the video embedded above for instructions on how to enable that.

Main will now ask you to implement the start() method required by the Application interface.  Go ahead and do that and then initialize the scene and layout variables within the method.  The Scene() arguments are a layout instance, window width and window height.

Add the following line to your main() method in order to initialize the JavaFX calls.

Finally, at the bottom of the start() method, you will need to set the scene of the passed Stage with .setScene(scene) and then make a call to window.show() at the bottom of the method.

 

If you found this tutorial helpful, consider showing your support here.

Current Patreon supporters at the Source Code tier and up can see the entire source code right below this line: (Login with your Patreon credentials here)