Evgeniy Sokolov
Evgeniy Sokolov

Reputation: 69

UTF-8 wrong character encoding with spring-boot-devtools

IDE settings UTF-8, OS Windows 10, Russian language. Code for console output:

System.out.println("тест")

Spring Boot without "spring-boot-devtools" dependency in IDEA is OK

Spring Boot with "spring-boot-devtools" in Windows console is OK

Spring Boot with "spring-boot-devtools" in IDEA problem: enter image description here

Upvotes: 4

Views: 3531

Answers (1)

CrazyCoder
CrazyCoder

Reputation: 402413

The following configuration in pom.xml will fix the problem:

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <executable>true</executable>
          <jvmArguments>-Dfile.encoding=UTF8</jvmArguments>
        </configuration>
      </plugin>
    </plugins>
  </build>

Explanation:

On Windows console encoding is cp1251 with your locale configuration, while IntelliJ IDEA console encoding is UTF-8.

When running from IntelliJ IDEA, -Dfile.encoding=UTF8 option is added to the command line when IDE is starting Maven so that Maven JVM output encoding is the same as the IDE console encoding.

Without spring-boot-devtools this option is passed correctly and is honored by the JVM process running your code in the IDE console. It's the same JVM instance that is running Maven, everything works fine.

With spring-boot-devtools dependency Maven JVM forks another JVM to run your code and file encoding option is no longer passed to it. File encoding defaults to the system locale for the new JVM process and becomes cp1251 while IntelliJ IDEA console is still set to use UTF-8 encoding. Output is broken as shown on your screenshot.

spring-boot-maven-plugin jvmArguments configuration should be used to pass the encoding option to the forked JVM. It also forces Windows console to the same encoding, so the app will continue to work correctly in the command line.

Upvotes: 6

Related Questions