Java encoding issues after upgrading to Java 17 using the Microsoft OpenJDK

4.7k Views Asked by At

After I upgraded to Java 17 using the Microsoft OpenJDK, all the tests that use non-ASCII characters are failing due to encoding failure issues.

For instance, one of my tests uses the following Unicode characters (e.g., U+2660 to U+2663):

entityManager.persist(
    new Suit()
    .setName("Club")
    .setSymbol("♣")
);

entityManager.persist(
    new Suit()
    .setName("Diamond")
    .setSymbol("♦")
);

entityManager.persist(
    new Suit()
    .setName("Heart")
    .setSymbol("♥")
);

entityManager.persist(
    new Suit()
    .setName("Spade")
    .setSymbol("♠")
);

How to fix it?

2

There are 2 best solutions below

2
Vlad Mihalcea On BEST ANSWER

While upgrading to the Java 17 version using the OpenJDK built by Microsoft, I also ran into problems because the Java source files were now encoded using the default Windows encoding, instead of UTF-8.

To fix the problem, set the file.encoding property to UTF-8.

The easiest way to do it is to set the MAVEN_OPTS environment variable:

MAVEN_OPTS=-Dfile.encoding=UTF-8

Another option is to pass it to the Maven Surefire plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin.version}</version>
    <configuration>
        <argLine>-Dfile.encoding=UTF-8</argLine>
    </configuration>
</plugin>

And, if you want to start a Java program, then pass the -Dfile.encoding=UTF8 property.

0
Bernie Lenz On

Related, but in a different environment than yours...

I had the same issue when upgrading AWS Corretto Java from version 11 to 17, where some of test cases with special characters suddenly started failing after the upgrade.

The fix for me was to add below to the aws codebuild buildspec.yml.

  pre_build:
    commands:
      - dnf -y install glibc-langpack-en
      - export LC_ALL="en_US.utf8"