3

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
Ayub
  • 599
  • 3
  • 10
  • 24
  • Possible duplicate of [spring boot application showing ??? characters instead of unicode](https://stackoverflow.com/questions/44992808/spring-boot-application-showing-characters-instead-of-unicode) – Abhijit Sarkar Jul 11 '17 at 07:04
  • I've updated my answer. – Abhijit Sarkar Jul 11 '17 at 07:31
  • This works on Windows under Eclipse and its console as long as you save the source file with UTF-8 encoding. If you try to save as 1252 then Eclipse will warn you that it's not going to work. I'd like to see a hex dump of the relevant part of your source file on Windows. I suspect it's not saved on disk as UTF-8. If it is OK on disk then the probable cause is that your Windows console is not capable of rendering UTF-8. – Andy Brown Jul 14 '17 at 08:19

3 Answers3

4

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

JC Carrillo
  • 986
  • 6
  • 17
  • 1
    tried on command line, Default Charset: UTF-8 is printed but the characters are still printed as ??? – Ayub Jul 11 '17 at 04:29
  • It helped, thank you! When running SpringBoot as a runnable jar, it was writing in Win-1252, but this options both logs and generated files are now in UTF-8. Great! – Dmitriy Popov Jan 21 '21 at 03:05
1

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:

public class Main {

    public static void main(String[] args) {
        System.out.println("Default Charset=" + Charset.defaultCharset());
        System.out.println("test::" + "الرياض");
    }
}

Default Charset=UTF-8
test::الرياض

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 not System.out.println.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
  • @JC Carrillo public static main prints correct unicode. java "-Dfile.encoding=UTF8" -jar target\my-app.jar prints below Default charset=UTF-8 test: ???? not sure which setting is causing it to print this way :( Below is the maven project, I am running this on windows 7: https://github.com/AyubOpen/spring-boot-jdbc.git – Ayub Jul 11 '17 at 04:06
  • Main class works, however the spring boot project does not, puzzling..pulling my hair :) – Ayub Jul 11 '17 at 04:13
  • This seems to be a windows issue, tried on ubuntu everything works. Have to figure out how to pass the correct encoding when running on windows – Ayub Jul 11 '17 at 20:00
  • @Ayub there's nothing to pass. Like I said in my edit, printing to stdout is not a real use case – Abhijit Sarkar Jul 11 '17 at 20:21
  • you are right printing std out is not real use case, this I was just debugging. 2The jdbc datasource I tried using debug, When I step through the debugger, I could see that just before printing when I evaluate the expression the unicode is correctly retrieved from the database. Its printing ??? only in sys out.Also print works without issues on ubuntu – Ayub Jul 11 '17 at 21:29
-1

@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 !!

Ayub
  • 599
  • 3
  • 10
  • 24