Sunday, May 30, 2021

Migrating Java 6/8 projects to JDK 11

Recently, I have migrated some of my applications from JDK 6 & 8 to JDK 11. As Oracle JDK 11 has a license cost associated with it; I have used Amazon Corretto JDK 11; which is a no-cost, multiplatform, production-ready distribution of OpenJDK.

Below are the steps followed for JDK 11 upgrade

Step 1: Install JDK 11 & Maven 3.6.3 & set the path

You can check the https://mkyong.com/java/how-to-set-java_home-on-windows-10/ for details of Java path settings

You can check the   https://mkyong.com/maven/how-to-install-maven-in-windows/ for detail of Maven path settings

Step 2: Next ,you need to do couple of changes in your application pom.xml:

If you have Maven compiler version set for JDK 6 or 8 in any of the below format; then remove those 

Format 1:

<properties>

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

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

</properties>


Format 2:

<plugins>

    <plugin>    

        <artifactId>maven-compiler-plugin</artifactId>

        <configuration>

            <source>1.8</source>

            <target>1.8</target>

        </configuration>

    </plugin>

</plugins>


We need to add the below compilation setting under <build> tag & surefire plugin settings should be modified as below:

  <plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.22.0</version>

<configuration>

<argLine>

--illegal-access=permit

</argLine>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.8.0</version>

<configuration>

<release>11</release>

</configuration>

</plugin>

</plugins>

Step 3: If your application is using Spring Framework 3.x or 4.x then you need to upgrade the Spring version to atleast 5.1.0.RELEASE. 

As 5.1.0.RELEASE is the compatible version with JDK 11. Otherwise, you will not get compile time exception but at runtime you will get exception like below:

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [MyClass.class]; nested exception is java.lang.ArrayIndexOutOfBoundsException: 11315

You can find more details in https://www.javagists.com/beandefinitionstoreexception-failed-to-read-candidate-component-class

Upgrading Spring version can give you comile time time error based on methods which has been removed from upper Spring version. E.g. Spring 3.x have methods in JDBCTemplate for queryForInt, queryForLong; which has been deprecated &  queryForObject is introduced from Spring 4.x

Also, if you are using Spring JDK Timer (org.springframework.scheduling.timer); then it needs to be upgraded to  Spring Quartz Scheduler.

Convert Java Project from Log4j 1 to Log4j2

Many times while working on old Java projects we find Log4j 1.x is used. But as the Log4j2 is the new one; hence to upgrade to Log4j2 we nee...