Skip to main content
blog title image

3 minute read - IntelliJ Java For Testers Maven

How to fix Java Language Level Issues

Jun 22, 2020

When using other people’s source code you might discover that you receive errors about java soure levels. These are easily fixed.

e.g. an error like.

"java: strings in switch are not supported in -source 1.6
(use -source 7 or higher to enable strings in switch)"

Or something similar.


NOTE: text of this article updated 20200622 to cover the properties amendments.

Define Language Levels in the pom.xml

I might tackle this in two ways. One for the command line and one for the IDE.

Usually, once I’ve changed the pom.xml and refreshed to accept the pom.xml changes, the IDE works fine and I don’t need any further changes.

Using the Compiler Plugin configuration

By default the maven compiler is set to Java 1.6 (it used to be 1.5)

I tend to want to use features starting in 1.8, so I generally set it to 1.8

Amending the compiler plugin properties in the pom.xml file should mean that when you import the project into your IDE it sets the language settings appropriately should run properly when you run “mvn clean compile”

As a Build Plugin configuration

For the command line I can add the source and target to the maven compiler plugin

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

As a property configuration

I noticed recently that it was possible to configure this in a more short hand way, by using the maven.compiler.source and maven.compiler.target versions in the properties section.

This means we don’t need to have the plugin section for the maven compiler for this simple configuration.

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

Possibly Adjust the Compiler Settings

In Settings we have the Compiler settings

  • IntelliJ Idea \ Preferences \ Build, Execution, Deployment \ Compiler \ Java Compiler
  • File \ Settings \ Compiler \ Java Compiler

We might need to set these up to use the level we need.

Adjust the Project Settings

When running in the IDE we might need to change a few things

In IntelliJ you find the language settings in a few places.

Project level we can define the SDK and the Language Level.

  • right click on project Open Module Settings \ Project
  • File \ Project Structure \ Project Settings \ Project

We can override the SDK or set the Project Language Level

Staying on Project we want to check the Platform Settings

And that we have an SDK of the required version setup and in use.

Check Modules Settings as well

You might even need to check in the Module settings on the sources tab

  • right click on project Open Module Settings \ Modules
  • File \ Project Structure \ Project Settings \ Modules