IntelliJ: evaluate lambda expression raises an compile error in debugging - intellij-idea

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?

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:
Install Java 8
Change IntelliJ to use Java 8. See these instructions.

Even after applying above defined project specific settings on IntelliJ as well as Eclipse, it was still failing for me !
what worked for me was addition of maven plugin with source and target with 1.8 setting in POM XML:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.abc.sparkcore.JavaWordCount</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

You can switch runtime version of JDK:
Start the IDE, Help | Find Action (Ctrl+Shift+A or Cmd+Shift+A on Mac), type "Switch IDE Boot JDK", press Enter.
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.

Unable to compile for target level 1.8...Currently running on version 1.6
My guess is that your project is being run on a version of Java that doesn't support lambdas. Make sure you have Java 8 installed, and also make sure to change your project level to use Java 8, like so:
Notice that it explicitly mentions lambdas

Related

IntelliJ IDE error java: warnings found and -Werror specified

I am getting below error in IDE (2020.3.4) in my java project when I tried to build/Run the project. I am not be able to build/run the application via IDE because of this issue.
java: warnings found and -Werror specified
I got below line in my POM file and tried to comment that as well. But same error.
<configuration>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<compilerArgs>
<!-- <arg>-Werror</arg>-->
<arg>-verbose</arg>
</compilerArgs>
<source>11</source>
<target>11</target> <!--or <release>10</release>-->
</configuration>
Is this any setting specific error.
I was facing the same issue, which I fixed by removing Werror flag from Intellij:
file > settings > Build, Execution Deployment > Java Compiler
At the bottom, you can find it under Override compiler parameter from there you can remove the option by clicking on - button
Also, verify if you are passing this flag to the maven-compiler-plugin and remove it if it is not needed.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerArgs>
<arg>-Werror</arg>
<arg>-Xlint:all</arg>
<arg>-Xlint:-deprecation</arg>
<arg>-Xlint:-options</arg>
<arg>-Xlint:-processing</arg>
<arg>-Xlint:-serial</arg>
</compilerArgs>
<showWarnings>true</showWarnings>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
I got below line in my POM file and tried to comment that as well. But same error.
Make sure you have removed this flag from all possible places in pom.xml files and invoked Reload action in the Maven tool window.
I was searching for -Werror and was getting no results back.
In a different form it can be without - as a tag in maven-compiler-plugin, example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments combine.children="append">
<Werror />
</compilerArguments>
</configuration>
</plugin>
Commenting out this piece helped.

Can't run and debug groovy tests under intellij idea

I try to embed groovy test to java project.
I start with spock examples - https://github.com/spockframework/spock-example
Examples is compile and execute by running maven goal test but if i try to run test under intellij idea (ctrl+F10 under test method) it failure with classpath error.
Error running HelloSpockSpec.length of Spock's and his friends' names:
Class 'HelloSpockSpec' not found in module 'spock-example'
I try to apply advices from IntelliJ + Groovy + Spock but it didn't help.
Don't forget to mark the folder as "Test Sources" in IntelliJ
Then it should work as expected :-)
Intellij can automatically add the groovy source as a source directory based on your pom. Add build-helper-maven-plugin config to your maven pom under plugins specifying ${basedir}/src/test/groovy as a source dir:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-groovy-test-source</id>
<phase>test</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/test/groovy</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

Intellij Idea 14.1.1problems evaluating lambda expressions

I am running evaluate expression for lambda expressions in my IDE,Intellij Idea 14.1.1.It gives me the message 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-466.1-11M4716
The code compiles just fine and runs fine too,only when i do evaluate expression while debugging do i get this problem.
I have checked all the compiler setting they are all set to 1.8.
Any ideas what setting i need to change?
Install java 8;
From Project Structure -> Project Settings ->
Project: select java 1.8 as Project SDK; select 8 - Lamdas, type
annotations etc.
From Settings -> Language & Frameworks ->
JavaScript -> Libraries: from the libraries list, make sure
ECMAScript 6 is selected.
Open pom.xml and enter Java configuration inside build:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

Maven annotation processing with maven-compiler-plugin

I try to compile my code that contains annotations that generate source code. I use the maven-compiler-plugin and build-helper-maven-plugin. My POM is looking like that:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<generatedSourcesDirectory>${project.build.directory}/generated-sources/apt</generatedSourcesDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/apt</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
When I run mvn compile, ${project.build.directory}/generated-sources/apt is added as a source directory, and the generated sources are generated in the correct directory. But I get compiler errors because of missing references to the generated classes. It's like the generated source directory is not included in the compilation process.
I also try apt-maven-plugin which does not generate anything. And maven-annotation-plugin but it behaves as describe above.
The release 2.0.7-SNAPSHOT of maven-annotation-plugin should solve the problem
fwiw I just downgraded from 3.0 to 2.5.1 and fixed an issue I was seeing with APT processing not being executed after a few runs, using maven so just downgraded one version:
http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.apache.maven.plugins%22%20AND%20a%3A%22maven-compiler-plugin%22
Can you try with the latest version of maven compiler plugin (2.3.2)?
Also build-helper-maven-plugin is not required since it looks like you are using the maven-compiler-plugin itself to generate the sources from the annotation.

Maven2: How to be sure Maven build is using a specific plugin version?

I just found something that sounds weird with Maven plugin management.
While working on the site generation I wanted to use a specific version of the maven site plugin in order to have a specific functionnalty working.
Let's say I want to use version 2.0.1 of this plugin.
If I use the reporting section of my POM in order to generate my project's site with the command:
mvn site
this works well. I mean the plugin version used is 2.0.1 as I wanted. Here is an extract from my POM configuring the site plugin:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>2.0.1</version>
</plugin>
</plugins>
</reporting>
Now if I want my site to be generated during a specific phase of the build life cycle, let's say prepare-package (and goal stage), I add the following section in the section:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>stage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
And here I am stuck with the maven site plugin version coming from the Super POM, ie. 2.0-beta-7.
Even if I try to add the configuration specifying I really want to use version 2.0.1 it still uses 2.0-beta-7.
I also tried to add the version in the section because the config that is used in the reporting section is supposed to be applied to the build section also. But this does not work neither.
Maybe I missed something, and correct me if I am wrong but this looks like a bug.
Is there a need on the Maven side to fix plugin's version to be used during the build process?
Thanks!
If you define a pluginManagement section in the pom, you can declare the versions used for any plugins, this will override the versions inherited from the super POM
For example:
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>2.0.1</version>
</plugin>
</plugins>
</pluginManagement>
You can refer to the documentation for some background on configuring pluginManagement.
I think you need to use the "pluginManagement" section to set the global version number of the plugin.