Monday, February 3, 2014

Eclipse Setup For JDK8 Lambda Development

As developers, we all love to get a jump on learning changes to the technologies we employ to make our living. With the release of JDK8 right around the corner, March 18, 2014 from the last I read, I thought I'd give a crack at learning how to use lambdas in my code.

So, I set straight to it. I went and got the latest JDK8 beta release, set it as JAVA_HOME, downloaded Eclipse Kepler and, much to my chagrin, it had no idea how to deal with my first lambda code example.

After a significant amount of effort and almost giving up, I finally got my environment working and could spend some time learning how to use this new feature coming in the JDK release. I figured because I blog about all sorts of things related in and around Java, that maybe others might benefit from some quick tips on how to set up their environment to test this new feature.

Getting Started

  • Download Eclipse Kepler and extract it only the files inside the zipped "eclipse" folder into the location of your choice.  For this blog's purpose we will reference C:\Kepler_Eclipse.
  • Download JDK8 with Lambda Support and install it. Note: You must accept the "Pre-Production Software Evaluation Agreement for Java SE" in order to begin the download.

Changing An Existing Kepler Configuration


If you already have Kepler installed, you need only make the changes to add the support for JDK8 shown in the software update and to make sure you have JDK8 as your project JRE, project compiler and JAVA_HOME. You can jump straight to the Testing Out Ecliplse Configuration section.

Make JDK8 Your Main Java Version

 Open your System Properties
  1. Press ΓΏ + R keys together
  2. Enter the following in the input box (without quotes "control sysdm.cpl,,3")
  3. Click OK
Image 1: System Properties Dialog box

Add or Update JAVA_HOME

  1. In the System Properties window displayed, click the Environment Variables button to display the Environment Variables dialog shown in Image 2.

    Image 2: Environment Variables Dialog

  2. As you can see, I develop with multiple versions of Java and need to switch my JAVA_HOME between them from time to time. I have set up the releases I run so I can easily change which one the system is using. You do not need to do this if you are using only one Java release.

    If JAVA_HOME is not in your System Variables section, you will need to click the "New..." button. If it is present and not currently pointing to JDK8, you will need to click it in the table to highlight it then click the "Edit..." button. The New (or Edit depending on which button was pressed) Environment Variable dialog box will open as shown in Image 3. Note the values for a new dialog are empty on first display.  I entered the values shown to use the image for multiple steps.

    Image 3: New System Variable dialog
  3. In the New Environment Variable Window, enter the the Variable name as "JAVA_HOME" and the value as the location to the Java folder (not java/bin) where you installed Java (i.e. "c:\jdk1.8.0"). The example above shows how I set mine up to reference my JDK8 instance I had assigned to the JAVA_1_8 environment variable I created.
  4. Click "OK"
  5. Before we can close the Environment Variables dialog, we need to modify the Path variable.  Scroll through the System Variables and find the "Path" environment variable and click the "Edit..." button.
  6. Referring to Image 4, left-click the cursor in the value field once and then using the left and right buttons, look through the value field for a %JAVA_HOME%\bin entry. If it is found, skip to Step 7. If it is not found, move the cursor all the way to the home (left arrow until cursor moves left no further or just press the "Home" key when the edit cursor is in the variable value) and enter, sans double-quotes, "%JAVA_HOME%\bin;" at the start of the Path value.

    Image 4: Edit Path System Variable

  7. Click OK
  8. Click OK in the Environment Variables dialog to close it.
  9. Click OK to close the System Properties dialog to close it.

Prepare Eclipse

  1. Open Eclipse by double-clicking the eclipse.exe icon in the C:\Kepler_Eclipse folder (or the folder where you extracted it to). If the extract was done incorrectly, the files for Eclipse may be inside an additional "eclipse" folder.
  2. In the Workspace Launcher dialog, enter the location where you would like to save your projects (or you can point it to an existing workspace).

    Image 5: Eclipse Workspace Launcher dialog
  3. Once Eclipse is open, from the menu, go to Help->Install New Software... (see Image 6)
    Image 6: Available Software dialog
  4. Type, without quotes "org.springsource.ide.eclipse.java8.site - http://dist.springsource.com/snapshot/TOOLS/java8/e43" into the "Work with:" field and hit "Enter". After a few seconds, the main listing of software found will be updated.
  5. Click Next to see the Install Details view.
  6. Click Next to see the Review Licenses view.
  7. Click "I accept the terms of the license agreement" and Click "Finish"
  8. If you get a Security Warning dialog about installing software that contains unsigned content, click "OK"
  9. When prompted to restart Eclipse, click "Yes"

Testing Out Eclipse Configuration


At this point, we are ready to try out the Eclipse configuration.  If you know how to use GIT and want to pull my code for this example directly, you can find the code in the java-blog-samples repository. As the subject of this blog is neither to teach how to import a project from GIT into Eclipse nor is it to teach how to use GIT, please refer to the excellent tutorial that can be found at vogella.com.

If you need to do the testing manually, here are the basic steps.
  1. Right click in the Project Explorer box in any blank area
  2. Select "New" -> "Project..."
  3. In the Filter Box, enter "Java Project" without quotes
  4. Select the Wizard that is just called "Java Project" and click "Next"
  5. In the new Java Project dialog, enter the project name "JDK8_Sandbox". (Refer to Image 7 below)

    Image : Create a Java Project
  6. Select the JRE option "Use an execution environment JRE and select "JavaSE-1.8". If it is not available, click the Configure JREs... link and add jdk1.8.0 to the list of JREs configured
  7. Click "Finish". A new project will be created.
  8. Create a new package in the src directory, given any name you want to call it
  9. Create a class in that package called "Sandbox"
  10. Select all lines but the package name and delete them.  Copy the following code and paste it in the Sandbox class

    import java.util.List;
    import java.util.function.Predicate;
    
    public class Sandbox {
     
     public static void main(String[] args) {
      List<TestObject> objList = new TestObject().generateTestObjectList();
      Predicate<TestObject> objFilter = tO -> tO.getA() > 20;
      objList.stream().filter(objFilter).forEach(tO -> tO.displayObject());
     }
    
    }
    
  11. Create a new class in the same package called "TestObject"
  12. Select all lines except the package name and copy and paste the following code into the TestObject class.

    package com.blogspot.howdoidothatinjava.sandbox;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestObject {
     int a, b;
     
     public TestObject(){
      a = 0;
      b = 0;
     }
     
     public TestObject(int a, int b){
      this.a = a;
      this.b = b;
     }
     
     public List<TestObject> generateTestObjectList(){
      List<TestObject> out = new ArrayList<TestObject>();
      int[] aArray = new int[]{0, 1, 2, 3, 5, 8, 13, 21, 34, 45, 79, 124};
      int[] bArray = new int[]{1, 3, 6, 10, 15, 21, 28, 37, 47, 58, 70, 82};
      for (int i = 0; i < aArray.length; i++){
       out.add(new TestObject(aArray[i], bArray[i]));
      }
      return out;
     }
     
     public int getA(){
      return a;
     }
     
     public int getB(){
      return b;
     }
     
     public void displayObject(){
      System.out.println("TestObject: a: " + getA() + " b: " + getB());
     }
     
    }
    
  13. Build the project (if not set to automatic) and any errors should be cleared.
  14. Switch to the Sandbox.java file tab again in Eclipse
  15. Right click on the code and Select "Run As" -> "Java Application"
  16. If you followed the configuration exactly as above, the following should be displayed in the Eclipse Console view:

    TestObject: a: 21 b: 37
    TestObject: a: 34 b: 47
    TestObject: a: 45 b: 58
    TestObject: a: 79 b: 70
    TestObject: a: 124 b: 82
    

Wrap-Up

As this has been intended to discuss setup of Eclipse Kepler to use for JDK8 development, I won't take more time to discuss more of the details of lambdas in JDK8.  I will cover that in a later blog post but for now, you are set up to do further learning and experimenting with JDK8.

No comments :

Post a Comment

All comments are moderated. NO ADS. NO SPAM.