Setting JAVA_HOME for Maven on Windows 10 - java

I recently upgraded to Windows 10. I'm trying to create a new maven project in eclipse, but it doesn't seem to be picking up my JAVA_HOME setting.
Here is the JDK installation directory:
And here is my JAVA_HOME environment variable setting:
If I run mvn -version in a command prompt, maven does seem to get the correct value for JAVA_HOME:
I then try to create a new maven project in eclipse, using a barebones pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>StaticVoidGames</groupId>
<artifactId>DatabaseTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DatabaseTest</name>
</project>
But the project's JRE is 1.5 for some reason:
For what it's worth, my eclipse workspace default JRE is the same as my JAVA_HOME setting.
I've tried manually changing the build path to use the correct jdk, but it simply reverts. I've tried cleaning the project, doing a maven update, restarting eclipse, restarting my computer, and creating a new project. Maven still chooses Java 1.5, which isn't even installed on my machine.
All of my googling says that maven should pick up the JAVA_HOME setting to figure out which Java to use, but that doesn't seem to be the case here. Is there another setting that I'm missing somewhere?

As per screenshoot, Maven is correctly picking up your JAVA_HOME, however what you see in Eclipse is not the version of Java installed (1.5) but the version Maven will use by default to compile.
In your case, Maven will use JDK 1.8 and compile settings source and target to 1.5 (version by default).
To set Maven to the same level as the Java version installed, you need to configure the Maven Compiler Plugin. Once done that and after an Eclipse update (right click on the project, Maven, Update Project), Eclipse will update its view accordingly.
You can add to your POM the following Maven Compiler Plugin configuration:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Alternatively, you can use well-known Maven properties to achieve the same by only settings:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

Related

Build failing due to version only via terminal

I have a simple maven project which builds fine and runs in Intellij. The Java version is set to version 8 under relevant settings in Intellij.
Under the maven POM file, I have set the following to point to the java version.
<modelVersion>4.0.0</modelVersion>
<properties>
<java.version>1.8</java.version>
<javac.src.version>1.8</javac.src.version>
<javac.target.version>1.8</javac.target.version>
</properties>
<groupId>org.example</groupId>
<artifactId>exa</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
.....
My maven version and info as follows:
Maven home: C:\Users\name\apache-maven-3.6.3\bin\..
Java version: 1.8.0_231, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_231\jre
As mentioned, no issues when run via Intellij. But when I run maven commands via the intellij terminal or normal terminal, I get following error.
Example Maven commands.
mvn clean package
or
mvn verify
Error:
COMPILATION ERROR
/C:/Users/name/projectname/src/main/java/pack/Main.java:[17,51] method references are not supported in -source 1.5
I don't even have java sdk 1.5 on my machine. Can I get some help on where I should change this version to 8 so I can make a maven build please? Thanks.
In your project's pom.xml, it seems the source is configured to java 1.5. Make sure the correct source version is configured in pom.xml.
If you are using maven-compiler-plugin, change the version as below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
or
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

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.

IntelliJ target bytecode reverting

For a project that I'm currently working on, IntelliJ gave me the compile error Error:java: javacTask: source release 8 requires target release 1.8. I went into Settings>Build, Execution, Deployment>Compiler>Java and saw that the target bytecode version for one of my modules was set to 1.5, so I changed it to 1.8, compiled, and it worked. But then the next day, I got the same error. I went into settings and the target bytecode for that one module was back at 1.5. I changed it to 1.8 and it compiled/ran just fine. This has now happened multiple times and I am frustrated by the number of times I have to go into settings to manually change the target bytecode version.
Why does the target bytecode version keep reverting? I don't have 1.5 specified in the pom or anywhere else, so I am baffled as to why the bytecode version keeps getting set to 1.5.
You need to specify the source and target versions in your pom.xml file, but since the maven-compiler-plugin is added by default, an easier way to do that would be to set the following properties:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
You need to dd this to your POM:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
FYI.
I still had this problem despite setting the pom.xml with both the settings above from the answers as mentioned by #isapir and #carlos-a-ibarra respectively.
I discovered this was due to an Intellij setting:
This config can be found in Build,Execution,Deployment > Build Tools > Maven > Importing
By default, it is set to the 'Internal JRE' setting, which in the latest version of Idea is 11. I had to select it to use the 1.8 JDK the project was configured to use.
This is annoying, because if you have a mix of 11 and 8 projects, you'll have to manually toggle this setting back and forth.
It caused the module output to always fall back to 11 every time the pom.xml was reimported.
It's a really annoying bug in IntelliJ.
Please try below option, it worked for me.
step 01: Change the version under Module and project.
step 02: build the module.
Hope it should resolve the issue.
I have the same issue with "IntelliJ IDEA 2022.2.3 (Community Edition)".
This is my "pom.xml"
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>java-test-17</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
</project>
I can manually change the "Target bytecode version" to 17. (Preference => Build, Execution, Deployment) => Compiler => Java Compiler).
However, if I reload maven project (Right click "pom.xml" => Maven => Reload project), the "Target bytecode version" will be changed back to 1.5.
To prevent that, I added "maven-compiler-plugin" to pom.xml. Then I reload maven project (Right click "pom.xml" => Maven => Reload project). Now, the "Target bytecode version" is 17.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>java-test-17</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
</plugins>
</build>
</project>

Java compiler level does not match the version of the installed Java project facet", but it does

I'm updating an ancient WebSphere 5 web appliction, porting it to run in Tomcat, replacing IBM RAD 7.0 with Eclipse Kepler, and using Maven for dependencies.
I am getting the "Faceted Project Problem (Java Version Mismatch)" error. The specific text is "Java compiler level does not match the version of the installed Java project facet.".
Here's where I've checked for compiler version:
I right-click on the project and select Properties. I click "Project Facets", and see my Java facet says 1.7.
I check the "Java Compiler" properties page (I'd like the "Enable project specific settings" box to remain unchecked, but it's checked and when I uncheck it, Maven->Refresh Project, and it's checked again.). The compiler version is grayed out, but it is also 1.7.
Window/Preferences/Java/Compiler is also 1.7, and the "Use default compliance settings" box is checked.
Windows/Preferences/Java/Installed JREs has both jdk1.7.0_71 (checked), and jre7 available.
pom.xml also says 1.7:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
...
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
...
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.opennms.core.snmp</groupId>
<artifactId>org.opennms.core.snmp.joesnmp</artifactId>
<version>0.2.6</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Where am I setting a Java compiler level that "does not match the version of the installed Java project facet"?
I had a similar problem before that I resolved by modifying the eclipse.ini file to ensure Eclipse will run the appropriate JVM.
This is what i added to eclipse.ini (you need to use your exact path to javaw.exe) before the -vmargs option:
-vm
C:/Program Files/Java/jdk1.7.0_71/bin/javaw.exe
I had similar case.. changing "Project Faces" brought no results. In my case helped right click on project -> properties -> java compplier and then uncheck option "use compliance from execution environemnt..." and choose your java version (in my case 1.8)

stop IntelliJ IDEA to switch java language level every time the pom is reloaded (or change the default project language level)

Using IntelliJ 12, I have a java project and I use maven with a pom.xml.
My project is using java8, but it seems the default project language level has been set to 6 while importing the project.
I can change the language level to 8.0 (F4 -> Modules -> Language level) however every time I edit my pom.xml the project level is switched back to "use project language level", and I have to edit this settings again and again.
Is there something I need to add to the pom.xml to set the default language level to 8.0?
As per Mark's comment, here is how to do it:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
A shorter version of vikingsteve's answer is:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
I'm upgrading a project from JDK 8 to JDK 10+. I had the compiler properties specified correctly as follows:
<properties>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
However the Idea project would keep resetting the language level to 8.
Eventually I figured out that Idea's Maven import process was using JDK 8 to import the project which limited the language level to <= 8.
To fix I updated the 'JDK for importer' property under Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Importing to use JDK 11.
I think this has to do with a conceptual conflict between the Maven compiler plugin and IntelliJ idea. Apparently the newer versions of the compiler plugin have a default level of 1.5 (see http://maven.apache.org/plugins/maven-compiler-plugin/). So if the compiler plugin is used at all in a project, and the compiler level is not explicitly set in the pom.xml, whenever the POM is re-processed the level will revert to the default.
So there is a conceptual conflict which is ignored by Intellij IDEA. The IDE still allows one to set the project and module settings, but provides no warning or feedback that this setting is controlled by pom.xml. Solutions would either be to explicitly allow overriding the POM compiler plugin setting (perhaps not wise because what then happens when you use maven on the command line), or to deactivate the controls in the IDE when this setting from the POM is in effect.
The solution at the present time is to set the desired compiler level in the compiler plugin in the pom, the re-import, rather than trying to set it in module settings.
There are two ways of doing this, add either one of them in your pom.xml file:
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>
Let me know if it helps.
None of the solutions helped in my case. I didn’t need to specify any Java version in my pom.xml.
I needed to open the <project-name>.iml file and change the JDK version there.
Original:
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<!-- ... ^ -->
<!-- ... | -->
Updated:
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<!-- ... ^ -->
<!-- ... | -->
This makes no sense at all. At no point have I specified a JDK version for Java 1.5.
What solved the issue for me was a list of updates:
In Project Structure > modules: changes the language level Java version to 8
In Project Structure > Project: Java version should be 1.8
In pom file, same changes specified in the responses above
In Settings > Java compiler > changed the bytecode versions to 8
In Settings > maven > importing > JDK for importer should be 1.8
I struggled a lot with this problem, due to building microservices with Dropwizard. Eventually I found out that I had my build properties in the wrong pom file (The main service's pom.xml).
So, even though the other packages are more like libraries, I were not able to use the Java 8 syntax.
When I refactored the build plugin into the "global" .pom.xml" file, all child containers were able to use the new syntax.
May help someone having issues with multi-container projects
thank you it works.
be careful not to make the same mistake I did.
if you have profiles, add the plugin in the right profile.
<profiles>
<profile>
<id>foo</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>bar</id>
.......
</profile>
<profiles>
For me the solution of updating the POM (plugins plus properties) to the required Java compiler version (1_7 in my case) worked.
However as the .iml file for each project was generated with original pom (with default compiler version of 1_5 as explained by someone above) has a JDK version of 1_5, this still overrides the pom version.
I deleted the .idea folder manually and imported the module into IntelliJ with a reimport from the updated pom. When I reimported the Module from updated POM,I could see that the iml files had the updated JDK version (1_7 in my case) .
There was one additional step I had to follow, in addition to setting the maven build properties, adding the maven-compiler-plugin, and modifying the Java version in the .iml file. (each documented already in the other answers). You also have to set the compiler version in the settings.
Problem: Expected Java version: 11 but Stuck at version: 8
I tried almost all the answers, still I was stuck with language_level 8 and nowhere in my project or in the Intellij IDE I found any trace that could relate to Java version 8.
Then I explored the pom.xml of the parent with no success but in the pom.xml of the grand-parent, I found that the release version is 8.
Solution:
Adding the following 3 lines in the properties made the difference.
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
</properties>

Categories

Resources