tearvisus
tearvisus

Reputation: 2033

How to change Java version for Maven in IntelliJ?

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.

Upvotes: 110

Views: 124867

Answers (12)

Rahul
Rahul

Reputation: 3509

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>

Upvotes: 7

Anuj Teotia
Anuj Teotia

Reputation: 1323

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>

Upvotes: 1

Rudy Vissers
Rudy Vissers

Reputation: 5477

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:

enter image description here

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 !

Do not forget to reload the 'pom'!

If you want to use Java 17 then don't pass '1.17' but '17'. If you don't, the following errors are displayed:

error: invalid target release: 1.17 Module JAVA_17_MASTER_CLASS SDK 17 is not compatible with the source version 17. Upgrade Module SDK in project settings to 17 or higher. Open project settings.

Upvotes: 41

Adel Helal
Adel Helal

Reputation: 670

If you don't want to modify the pom file and you want to make it general for all mvn runs without having to set it for a specific mvn command, you may want to update the settings for the Maven Runner:

enter image description here

Upvotes: 0

Christian Vincenzo Traina
Christian Vincenzo Traina

Reputation: 10384

You can click on the maven view in the extreme right, then Lifecycle -> right click on install and Modify Run Configuration:

1

Then expand Java Options, disable Inherit from settings and select your desired Java version.

2

Upvotes: 1

papigee
papigee

Reputation: 7378

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

Upvotes: 77

Tohid Makari
Tohid Makari

Reputation: 2484

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.

Upvotes: 4

user11160452
user11160452

Reputation: 43

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

Upvotes: 1

Pankaj gupta
Pankaj gupta

Reputation: 1

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.

Upvotes: 0

madroid
madroid

Reputation: 334

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"

Upvotes: 0

Amit Kaneria
Amit Kaneria

Reputation: 5808

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

Upvotes: 4

hamid
hamid

Reputation: 1838

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>

Upvotes: 132

Related Questions