20

I use spring-boot in my project, and I run this jar file which is built by spring-boot as a service on Centos. When I run this service:

service myApp start

I always get the below error messages:

2016-08-26 09:11:02.002 ERROR 31900 --- [           main] o.s.b.c.FileEncodingApplicationListener  : System property 'file.encoding' is currently 'ANSI_X3.4-1968'. It should be 'UTF-8' (as defined in 'spring.mandatoryFileEncoding').
2016-08-26 09:11:02.018 ERROR 31900 --- [           main] o.s.b.c.FileEncodingApplicationListener  : Environment variable LANG is 'null'. You could use a locale setting that matches encoding='UTF-8'.
2016-08-26 09:11:02.018 ERROR 31900 --- [           main] o.s.b.c.FileEncodingApplicationListener  : Environment variable LC_ALL is 'null'. You could use a locale setting that matches encoding='UTF-8'.
2016-08-26 09:11:02.031 ERROR 31900 --- [           main] o.s.boot.SpringApplication               : Application startup failed
java.lang.IllegalStateException: The Java Virtual Machine has not been configured to use the desired default character encoding (UTF-8).
    at org.springframework.boot.context.FileEncodingApplicationListener.onApplicationEvent(FileEncodingApplicationListener.java:74) ~[spring-boot-1.3.7.RELEASE.jar!/:1.3.7.RELEASE]

If I run this jar file directly, then this application runs properly.

java -jar target/myApp-1.0.jar

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.7.RELEASE)

......

2016-08-26 09:54:34.954 DEBUG 32035 --- [           main] o.s.w.s.resource.ResourceUrlProvider     : Found resource handler mapping: URL pattern="/**", locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@1817d444]
2016-08-26 09:54:35.051  INFO 32035 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8000 (http)
2016-08-26 09:54:35.053 DEBUG 32035 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Adding [server.ports] PropertySource with highest search precedence
2016-08-26 09:54:35.061  INFO 32035 --- [           main] co.nz.myApplication           : Started myApplication in 12.339 seconds (JVM running for 13.183)

This is pom.xml:

<project>
....

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.7.RELEASE</version>
    <relativePath/>
 </parent>

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
 </properties>
 ....

 <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
                <jvmArguments>-Dfile.encoding=UTF8 -Dspring.profiles.active="production"</jvmArguments>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>springloaded</artifactId>
                    <version>1.2.5.RELEASE</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
 </build>
</project>

application.properties:

....
spring.mandatory-file-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
....

locale setting on Centos:

$ locale

LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

I check all files setting as possible as I do, however I still have no idea how to solve this problem. I would appreciate if you can give me any suggestion.

Coding minion
  • 527
  • 2
  • 4
  • 12
  • Create a FilterRegistrationBean Please see: [character encoding filter](https://stackoverflow.com/questions/24054648/how-to-configure-characterencodingfilter-in-springboot) – Kuldeep Singh May 16 '19 at 13:04

4 Answers4

17

SOLUTION

Add to config file (for application /var/app/app.jar it will be /var/app/app.conf) below line:

export LANG='en_US.UTF-8'

CAUSE

The problem is in linux systemd (service). I tested it with script /etc/init.d/test:

#/bin/bash
locale

result of command $ /etc/init.d/test:

LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

and result of command $ service test:

LANG=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

In spring boot jar file is build-in systemd script and before running spring boot is executed among others:

# Source any config file
configfile="$(basename "${jarfile%.*}.conf")"

# Initialize CONF_FOLDER location defaulting to jarfolder
[[ -z "$CONF_FOLDER" ]] && CONF_FOLDER="${jarfolder}"

# shellcheck source=/dev/null
[[ -r "${CONF_FOLDER}/${configfile}" ]] && source "${CONF_FOLDER}/${configfile}"
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Krzysztof Szewczyk
  • 1,752
  • 1
  • 21
  • 25
5

Based on my answer here: https://stackoverflow.com/a/48952844/986160 you need to run:

mvn spring-boot:run -Drun.jvmArguments="-Dfile.encoding=UTF-8"
Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106
  • 2
    I have tried your command but doesn't work. It has worked `java -Dfile.encoding=UTF-8 -jar khar.jar` – M. Rostami Feb 27 '20 at 07:59
  • this is for jar applications not for spring boot - I dont have jars in mine – Michail Michailidis Feb 27 '20 at 09:00
  • Downvote as running this Maven command is meant only for your local environment. For this answer to work for op they would need to install Maven inside the server and you should never do this as Maven is a build tool for devs, not operations tool. Inside the server environment everything should be run with java -jar command, whether it is directly or indirectly (via service start command or such). – Jukka Hämäläinen Mar 01 '23 at 17:26
2

Finally I found a way so that I can launch Spring-boot project properly.

The solution is use sudo /etc/init.d/myApp start instead of service myApp start

Coding minion
  • 527
  • 2
  • 4
  • 12
0

The first line tells you that you should set system property 'file.encoding'. I think that's the problem. Please see: Setting the default Java character encoding?

Community
  • 1
  • 1
Dominik Koszkul
  • 85
  • 1
  • 1
  • 5