Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot commandline runner is using windows default character encoding

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
like image 998
Ayub Avatar asked Feb 15 '26 20:02

Ayub


1 Answers

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

like image 118
JC Carrillo Avatar answered Feb 17 '26 12:02

JC Carrillo