Simple Java 11 Maven Project with Intellij IDEA


In this post we are going to create a simple Hello World in Java11 using maven and Intellij IDEA.

We are also going to deal with the compilation error “Error: java: error: release version 5 not supported” and the compilation warning “File encoding has not been set, using platform encoding UTF-8”

Video

Create the HelloWorld Project

  • Open Intellij IDEA
  • Click: Create New Project
  • Project SDK: Java 11

if not there

  • click in the Java Version drop down
  • Add JDK
  • Click on Java 11 folder
  • Click Open

Create a Maven Project

  • Maven
  • Do not check Maven Architype
  • Next

Enter Name and Artifact Coordinates

  • Name: Hello World
  • Artifact Coordinates:
  • GroupId: com.skills421.examples
  • ArtifactId: Hello World
  • Version: 1.0-SNAPSHOT
  • Click Finish

Create the Java Code

Create the java package

  • Navigate to src/main/java
  • right-click on the java folder
  • New -> Package
  • com.skills421.examples

Create the HelloWorld class

  • right-click on the package
  • New -> Java Class
  • HelloWorld

src code:

package com.skills421.examples;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Run the Code

  • right-click in the code panel
  • Run “HelloWorld.main()”
  • Note the build error: “Error: java: error: release version 5 not supported”

Modify the Project Object Model to Use Java 11

  • Open pom.xml
  • add the build section
<?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>com.skills421.examples</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • Click the square icon in the bottom left of the Intellij IDEA main window
  • select Maven
  • this opens a Maven panel on the right
  • Click the circular arrows to reload your maven project

Re-run the code

  • Go back to the Java code
  • right-click and run
  • The code now compiles and runs

Output:

Hello World

Clean and Build using Maven

  • Go back to the Maven window
  • expand HelloWorld -> Lifecycle
  • click on clean
  • click the green run icon in the maven panel

this cleans the project, removes all built code and removes the target directory

  • click on install
  • click the green run icon in the maven panel

this compiles and builds the whole project

note the compilation warning: “File encoding has not been set, using platform encoding UTF-8”

Modify the POM file again

  • Go back to pom.xml
  • add the properties section before the build section as follows:
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

  • click on install
  • click the green run icon in the maven panel
  • this compiles and builds the whole project

Now there are no errors and no warnings

Advertisement

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s