I am running spring boot application on windows and its using windows-1252 encoding. However stand alone java program is using UTF-8 encodeing. How do I force spring boot to use UTF-8. below code outputs ????. I use the below command using jar -jar target\spring-boot-example.jar
I verified that in power shell program that default character set is windows-1252 (System.Text.Encoding)::Default
public class SpringBootConsoleApplication implements CommandLineRunner {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootConsoleApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("Default Charset=" + Charset.defaultCharset());
System.out.println("test::" +"الرياض");
}
}
I tried the below options in application.properties without success:
# Charset of HTTP requests and responses. Added to the "Content-Type" header if not set explicitly.
spring.http.encoding.charset=UTF-8
# Enable http encoding support.
spring.http.encoding.enabled=true
# Force the encoding to the configured charset on HTTP requests and responses.
spring.http.encoding.force=true
The problem seems to be your stdout, not the code. I suggest you create a simple
main
class, without Spring Boot, or any external dependencies, and print UTF-8 characters. I'm on a Mac, and the following code prints find for me:Another thing you may want to try is writing it to a file instead of stdout.
Edit:
I think you're barking the wrong tree. No one uses Spring Boot for printing to stdout. The
spring.http.encoding
properties you'd set are actually HttpEncodingProperties which Boot uses to auto configure encoding using HttpEncodingAutoConfiguration. But all that's for a HTTP request, not for printing to stdout. Any application that deserves to go to Prod should use a logger, and notSystem.out.println
.Try running your app with
-Dfile.encoding=UTF8
in the command line.Example:
java -jar -Dfile.encoding=UTF8 target/myapp-0.0.1-SNAPSHOT.jar
@AbjitSarkar @JC Carrillo this issue seems to be with windows environment. I deployed the same code on UbuntuVM and everything seems to be fine.
Thank you for listening to me and giving me tips to explore !!