Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 356
Vlad Mihalcea Avatar asked Sep 05 '25 04:09

Vlad Mihalcea


1 Answers

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.

like image 60
Vlad Mihalcea Avatar answered Sep 07 '25 21:09

Vlad Mihalcea