Intellij Idea 14.1.1problems evaluating lambda expressions - intellij-idea

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>

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.

Tell IntelliJIdea to use specific JDK version, not just JDK8, but jdk-8u201

After a switch to JDK8 the MANIFEST.MF Attributes started returning null
https://bugs.openjdk.java.net/browse/JDK-8201636 suggests that this is a bug introduced by Oracle in JDK 8u151-8u172.
I use pom.xml and IntelliJ IDEA. pom.xml specifies (tags < and > removed)
<maven.compiler.target> 1.8 </maven.compiler.target>
<maven.compiler.source> 1.8 </maven.compiler.source>
IDEA settings show target bytecode version 1.8
JAVA_HOME set to JDK10
I have C:\Program Files\Java\jdk1.8.0_201 installed.
How to specify this version for the builds. Also does pom.xml trump IDEA project settings or vice versa?
Edit:
I've specified
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<!--executable>{JAVA_1_8_HOME}/bin/javac</executable-->
<executable>C:/Program Files/Java/jdk1.8.0_201/bin/javac</executable>
<compilerVersion>1.8</compilerVersion>
</configuration>
</plugin>
Intellij still defaults to JDK 1.5 and chokes on List<String> a = new ArrayList<>(); with "I don't understand <> with JDK1.5".
maven.compiler.target and source are nothing to do with it. Those are just arguments passed to javac.
If you want to specify the version of javac explicitly, you can use the Maven Compiler Plugin.
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable><!-- path-to-javac --></executable>
<compilerVersion>1.3</compilerVersion>
</configuration>
Otherwise the javac from the Project SDK will be used (Project Structure > Project).

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?
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

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.

Maven 2 checkstyle plugin version 2.5 - Problem with configLocation

I am using checkstyle plugin in maven 2. I now want to switch my config file, from the default one to a) an online file, or b) a local file. I tried the following two things, which both didnt work. Any suggestions?
A) Local file, which is directly in my project folder next to the pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
</plugin>
B) Remote file, that is stored on a server
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>http://stud.hs-heilbronn.de/~nischmid/development/checkstyle-config.xml</configLocation>
</configuration>
</plugin>
Both cases result in an error like this:
[INFO] An error has occurred in
Checkstyle report generation. Embedded
error: Failed during checkstyle
execution Could not find resource
'file:checkstyle.xml'.
Any help would be appreciated!
I've seen several issues related to configLocation in Jira with the version 2.5 of the plugin (like MCHECKSTYLE-129 or MCHECKSTYLE-131), both a) and b) just work fine with the version 2.4.
So, unless you're using Maven 3, I suggest to rollback to 2.4 for now:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.4</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
</plugins>
or
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.4</version>
<configuration>
<configLocation>http://stud.hs-heilbronn.de/~nischmid/development/checkstyle-config.xml</configLocation>
</configuration>
</plugin>
As a side note, for a multi-modules build, have a look at the Multimodule Configuration.
I've been trying to use version 3.0.1 of the Checkstyle plugin and found configLocation has no effect. Tried the approach above, but still no luck.
To summarise, the answer above probably does work, but you might need to set a property checkstyle.config.location.
Using -X to get debug output, I saw:
[DEBUG] (f) configLocation = config/sun_checks.xml
Scrolling further back in the log, it looks like configLocation isn't being set:
<configLocation default-value="config/sun_checks.xml">${checkstyle.config.location}</configLocation>
Based on that message, I set the property in the global <properties> as follows:
<checkstyle.config.location>${basedir}/config/checkstyle-configuration.xml</checkstyle.config.location>
This worked, but caused the plugin to throw an exception. After some Googling, I added the following to the checkstyle configuration file:
<module name="Checker">
...
<module name="TreeWalker">
...
<property name="cacheFile" value=""/>
For completeness, the last step came from the following Jira, marked as resolved in 2.8. The difference is it seems to work with an empty value, avoiding the need to set up a ${cachefile} property:
http://jira.codehaus.org/browse/MCHECKSTYLE-159
Maybe helpful for someone else who needs to still find a workaround.
By the way i had the same problem and the file is suppose to be searched in /classes/.xml or folders from here. But since it is looking directly after the project folder i included
<configuration>
<configLocation>src\main\resources\checkstyle-checker-dev.xml</configLocation>
</configuration>
Note: configLocation has L caps
Also you can define a global variable in environment and use here
Note: This is only a workaround, it needs to work as stated in the above lines.