Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ: evaluate lambda expression raises an compile error in debugging

I'd like to evaluate code including lambda expression with Intellij 'Evaluate Code Fragment' feature. But then, Intellij raises an error Unable to compile for target level 1.8. Need to run IDEA on java version at least 1.8, currently running on 1.6.0_65-b14-462-11M4609

The evaluating code is very simple as below.

Set<Integer> set = new HashSet<>();
set.add(1);
set.stream().map(v->v).collect(Collectors.toSet());

My Intellij version is 14.0.3 and according to official document, version 14 supports lambda expression evaluation.

How can the feature be available?

enter image description here

like image 230
Light Yagmi Avatar asked Apr 10 '15 07:04

Light Yagmi


People also ask

How do you evaluate lambda expression in IntelliJ?

You can use the Run to cursor function on a lambda expression as well. You can use Ctrl+F8 to add a breakpoint. Then the lambda expression that the cursor is placed on will be selected in the list. Pressing Ctrl+F8 again will complete creating a breakpoint on the selected lambda.

How do you evaluate expressions in IntelliJ while debugging?

Evaluate a complex expression in the editorClick Run | Debugging Actions | Quick Evaluate Expression Ctrl+Alt+F8 .

How do you Debug a lambda expression?

We can use different IDE's like Netbeans, IntelliJ, and Eclipse to debug the lambda expressions in Java. It is always possible to create multi-line lambda expressions and use print statements to display the values of a variable. The debugger can also provide additional information about the state of a java program.

What is the shortcut for evaluating the value of an expression?

Alt + F8 is the shortcut for evaluation of an expression or variable.


2 Answers

Putting together the comments from adrian lange and Bohuslav Burghardt, either upgrade to the latest IntelliJ 14.1 which comes bundled with Java 1.8, or switch your current IntelliJ to use Java 1.8. See bug IDEA-132099.

To switch to Java 1.8:

  1. Install Java 8
  2. Change IntelliJ to use Java 8. See these instructions.Project structure
like image 148
Kirby Avatar answered Nov 03 '22 10:11

Kirby


You can switch runtime version of JDK:

  1. Start the IDE, Help | Find Action (Ctrl+Shift+A or Cmd+Shift+A on Mac), type "Switch IDE Boot JDK", press Enter.
  2. Start the IDE, use Help | Find Action (Ctrl+Shift+A or Cmd+Shift+A on Mac), type "Switch IDE Boot JDK", press Enter. Switch IDE Boot JDK dialog appears. Select the version from the drop-down or click "…" entry to specify the custom location.
like image 40
grolegor Avatar answered Nov 03 '22 11:11

grolegor