Java 8 in Eclipse (Juno)

Since last July the new Java 7 is generally available. However, even the newer Java 8 is already available as an Early Access Preview. It can be download as part of the OpenJDK including lambda-support. To use it with Eclipse, some additional steps are required. Here’s how it goes:

Change some project settings in Eclipse in order to use the new javac compiler of Java 8:

Click right on your project and select Properties.

Select Builders on the left side and uncheck the default Java Builder.

Click on New and select Program as the type of the new builder.

Open javac from the JDK 8 on your disk (it’s in the folder bin).

Configure the new builder by providing some arguments:

1

2

3

-source8

-d${workspace_loc:/Java8}/bin

${workspace_loc:/Java8}/src/de/tuhrig/java8/*.java

Select the tab Build Options and check the During auto builds box, to let the builder run automatically.

Done! Now you can write and execute Java 8 code!

The main feature of Java 8 is the ability to write lambdas and use a functional-like programming style. This can be very useful, especially in GUI-programming. It can reduce the amount of code, because anonymous classes that only implements a listener-interface are avoided. Here’s a very simple example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

publicclassTest{

publicstaticvoidmain(String[]args){

JFrame frame=newJFrame();

frame.setLayout(newFlowLayout());

frame.setVisible(true);

JButton button1=newJButton("Click me!");

JButton button2=newJButton("Click me too!");

frame.getContentPane().add(button1);

frame.getContentPane().add(button2);

button1.addActionListener(Test::doSomething);

button2.addActionListener(e->{System.out.println("Bye!");});

}

publicstaticvoiddoSomething(ActionEvente){

System.out.println("Hi!");

}

}

The code creates a common JFrame with two buttons. To each button an action-listener is added. But instead of implementing an anonymous class, a method reference is passed in the first case and a lambda in the second case. This reduces the amount of code compared with the old solution that looked like this:

1

2

3

4

5

6

7

8

button.addActionListener(newActionListener(){

@Override

publicvoidactionPerformed(ActionEvente){

System.out.println("...");

}

});

However, the Java compiler will create exactly such a thing of the lambda expression or function reference. Therefore, the passed method must fulfill the interface described by ActionListener. Also, we can only use lambdas instead of interfaces that only describe one single method. Such interfaces are called functional interfaces.

Since Eclipse Juno is made for Java 7 it will not recognize the new lambda syntax. Hence, the editor will mark parts of the code as red errors. But it’s still possible to compile and to execute the code using the new javac compiler form Java 8.

I’m having a small problem with step 5. Could someone please break down for me what
-d ${workspace_loc:/Java8}/bin ${workspace_loc:/Java8}/src/de/tuhrig/java8/*.java means?
I’m assuming it’s the path of the workspace and the path of the java 8 download but I’m not sure because when I change it to suit my locations on my computer I’m getting errors saying Javac: file not found.

The argument -d ${workspace_loc:/Java8}/bin ${workspace_loc:/Java8}/src/de/tuhrig/java8/ is not the location of your JDK. It is your Eclipse project. You got a workspace (${workspace_loc:/Java8}) which contains a project called Java8. Inside this project you have a bin folder which contains the compiled results of your Java source file (which are located in /src/de/tuhrig/java8).

By the way: This article is very old. If you use the latest version oft Eclipse, you don’t need to do this stuff anymore.

Hi currently i am working with ecipse juno under cloudsim. After updating the java to version 8, when i try to run the eclipse from my dekstop, it is not running. whats the problem? could you please rectify it.