Maven Java error compiling on build: maven-compiler-plugin - java

When I try to run as maven build it always give BUILD FAILURE.
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project testJPA: Fatal error compiling: invalid target release: 11 -> [Help 1]
I already tried the following things:
remove .m2 folder
reinstall eclipse
set executable on pom.xml
set build option to clean
> java -version
java version "1.8.0_261"
> echo %JAVA_HOME%
C:\Program Files\Java\jdk1.8.0_211
pom.xml
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
None of them worked. Any guesses?

I managed to find a solution.
I installed JDK 11 and set it at Window > Preferences > Java > Installed JREs > Add > jdk-11.0.9
Downloaded at: https://www.oracle.com/java/technologies/javase-jdk11-downloads.html

Related

Could not find artifact jdk.tools

I am migrating my code from Java 1.8 to Java 11. I have done the below change in my pom.xml
From:
<java.version>1.8</java.version>
To:
<java.version>11</java.version>
Also changed the source and target in maven compiler as per below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
When I execute the command maven install I get the below error:
[ERROR] Failed to execute goal on project store: Could not resolve dependencies for project. Could not find artifact jdk.tools:jdk.tools:jar:1.7 at specified path C:\Program Files\Java\jdk-11.0.12/../lib/tools.jar -> [Help 1]
How do I solve this error? Please help

Change Java Version from 1.8 to 1.6 in maven project in intellij [duplicate]

I'm new to both Maven and IntelliJ IDEA.
I have a Maven project written in Java 8. Whenever I try to build it (Maven Projects window -> Lifecycle -> compile -> Run Maven Build) I get a series of compilation errors:
[ERROR] path/to/file.java:[26,52] lambda expressions are not supported in -source 1.5
(use -source 8 or higher to enable lambda expressions)
Where am I supposed to change the value of the -source parameter? I tried adding it as an additional parameter in Settings -> Compiler - > Java Compiler, but I got the same results.
The project's and module's language levels are both set to 8.0.
I'm using Maven 3.2.3 and IntelliJ IDEA Community Edition 13.1.2.
Or easier, add this to your pom's properties section:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
I don't think any response to the question addressed the concern - ". . . in IntelliJ".
Here are the steps: -
Go to Preference(or Settings) in IntelliJ ( or Shortcut on Mac ⌘ + ,)
Build, Execution, Deployment > Build Tools > Maven > Importing - select the "JDK for Importer" dropdown then select your preferred java version, Click Apply
Build, Execution, Deployment > Maven > Runner - select the "JRE" dropdown then select your preferred java version, Click Apply
Click OK
Summary:
'maven-compiler-plugin' ALWAYS work! It is what I suggest you to use.
To change the language level, make usage of
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
</plugins>
</build>
The properties do not always change the language level of Intellij!
In the code below, 1.4 was configured in the pom using maven-compiler-plugin
(the jdk of Intellij is 1.8) and the language level of the project was changed accordingly to 1.4:
It was double-checked! It is an example. Most of the time you will not downgrade the version of the JDK to 1.4!
Of course if you use properties, let's say you put in the pom 1.8, then if you use a 1.8 JDK in Intellij(the language level default is 1.8 or the language default was changed manually), then you will be able to code in 1.8 BUT at the mvn compile, the properties will NOT be seen and you will default to Maven 1.5 and the compilation will NOT succeed !
Change the source as shown below in pom.xml
<build>
<finalName>MQService</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Adding below lines to root(project level) pom.xml worked me to resolve above issue: (both of the options worked for me)
Option 1:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Option 2:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
originally posted at: IntelliJ IDEA 13 uses Java 1.5 despite setting to 1.7
It may sometimes happen that after configuring maven in Intellij and changing as following it does not work by command build, so build it by Intellij maven tool.
Setting > Maven > Importer - select the `JDK`
Setting > Maven > Runner - select the `JRE`
After that, try to build by Intellij maven tool instead of Intellij console.
There are two ways of doing this :
First- Add Properties
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
second- Add Plugin
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
Here are the steps for intellij version# 2021.2 Ultimate Edition: -
Go to Settings in IntelliJ
Build, Execution, Deployment > Build Tools > Maven > importing > "JDK for Importer" then select your preferred java version, Click Apply
Build, Execution, Deployment > Build Tools > Maven > Runner > For "JRE" option select your preferred java version,
Click Apply
Click OK
You should add below code in your pom.xml to force the language level to change
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
now intelliJ 2019.3 CE does this for you if you go to import then alt+enter where you will get an option saying "change language level to 8 to use this functionality"
open the ubuntu terminal goto your root directory and type:
export JAVA_HOME = <path to jdk>
for example, it works fine for me {do the same in IntelliJ terminal}.
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
to check the set value type echo $JAVA_HOME
to check the maven version type: mvn -version
you can find all path of JDKs by typing this command, and you can set JDK version.
sudo update-alternatives --config java
also check you have same java -version and javac -version.
You can click on the maven view in the extreme right, then Lifecycle -> right click on install and Modify Run Configuration:
Then expand Java Options, disable Inherit from settings and select your desired Java version.

tools.jar issue Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin

Running:
mvn install
Getting this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-
plugin:2.5.1:compile (default-compile) on project 1: Fatal error
compiling:
tools.jar not found: C:\Program Files\Java\jre1.8.0_131\..\lib\tools.jar ->
[Help 1]
Area of POM file error:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
When I look at C:\Program Files\Java I'm not seeing tools.jar under JRE lib folder. I see it under JDK lib folder
You are using a JRE, please use a JDK compile your code
How to set a java compiler in Netbeans

setting JAVA_HOME for Maven ONLY regardless of my code java

I am running different java projects. Understanding that changing java_home system environment each time I switch between them is a bad idea, I'm starting to set JAVA_HOME specifically for each one of them independently.
When I set JAVA_HOME for maven (version 2) runtime (the java maven uses to run, being a java application)?
To my understanding, JAVA_HOME for maven should not affect how my code is compiled and run (which are supposed to be configured using maven-compiler-plugin source and target). Right?
When I set JAVA_HOME in mvn.bat:
#REM Maven2 Start Up Batch script
set JAVA_HOME=C:\dev\tools\jdk-1.6
And my maven-compiler-plugin is configured this way:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
I receive this error:
[ERROR] BUILD ERROR
[INFO] --------------------------------------
[INFO] Fatal error compiling
Embedded error: invalid target release: 1.7
Why does JAVA_HOME specified for maven runtime affect my code compilation? How to separate between the two?
Maven will use the JRE specified in JAVA_HOME
By default it will use the JDK from the same folder for compiling .java files
However you can change it in maven-compile-plugin configuration
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable><!-- path-to-javac --></executable>
<compilerVersion>1.3</compilerVersion>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>

error compiling java7 project with maven (heroku)

I'm trying to upload on heroku simple servlet with maven. Locally my servlet is working just fine but when i use:
git push heroku master
I get "BUILD FAILURE" with error message:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:
3.1:compile (default-compile) on project HelloServlet: Fatal error compiling:
invalid target release: 1.7 -> [Help 1]
I changed everything to Java 1.7 in system variables, maven is running Java 1.7, javac version is 1.7?
Am I missing something here?
edit: my JAVA_HOME and error screenshot
By default heroku apps run on OpenJDK 6.You have to add additional properties to make your app use Open JDK 7 on heroku.
Refer : https://devcenter.heroku.com/articles/add-java-version-to-an-existing-maven-app
Did you set source and target configuration parameters of maven-compiler-plugin correctly?
http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
In your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

Categories

Resources