1

I've spent almost 3 days to find out why in a big project the output of

System.out.println( "你好" );

is shown as "??" and in another project built-from-scratch it is shown as "你好".

I am using IntellijIdea, BUT the same thing happens to me while running it from a jar-file using

java -jar ...

The only two differences between those 2 project is in build system: first one uses gradle and second one uses none (just a simple project). The second difference is about output of the following code

     System.out.println("Charset.defaultCharset=" + 
     Charset.defaultCharset());

The first one types "Charset.defaultCharset=windows-1252", second one "Charset.defaultCharset=UTF-8"

Well, I HAVE read about:

  1. the encoding in IntellijIdea, all settings between 2 projects are the same (Settings → File Encoding → Project Encoding → IDE Encoding)

  2. About the encoding of file setting in the right lower corner in Idea

  3. Tried -Dfile.encoding=UTF-8 and then -Dconsole.encoding=UTF-8 in debug configurations for all gradle instances
  4. in cmd console did

    set JAVA_TOOL_OPTIONS=-Dconsole.encoding=UTF8
    
    set JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8
    
  5. Played with gradle properties in gradle.build

    org.gradle.jvmargs='-Dfile.encoding=UTF-8'
    systemProp.file.encoding=UTF-8
    
  6. changed the the coding in command prompt via

    chcp  65001
    

Those efforts bring me to nothing. Any ideas ?

BTW THERE IS almost the same question but with no answer. I cannot write there cause of low reputation Intellij Idea incorrect encoding in console output

the picture with settings in project that don't correctly display the output

ValerianTi
  • 81
  • 2
  • 11
  • 1
    have you seen this ? https://stackoverflow.com/questions/21267234/show-utf-8-text-properly-in-gradle –  Aug 31 '18 at 09:58
  • Yes, I've tried compileJava.options.encoding = 'UTF-8' tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } with no luck – ValerianTi Aug 31 '18 at 10:15

2 Answers2

1

Seems like you have done most of what i thought of 1...5 if the issue still persist, try restarting the ide or the machine.

Try that and let see...

yormen
  • 188
  • 2
  • 11
  • well, it's not only me, but others people meet such problems with that project. But It's me who have to fix that issue ;-) – ValerianTi Aug 31 '18 at 10:17
  • You have done all the five step, but no outcome? Could check the encoding in IntellijIdea, (Settings → File Encoding → Project Encoding → IDE Encoding) to see if the encoding has changed to UTF-8 – yormen Aug 31 '18 at 10:26
  • no luck, yep. the output of Charset.defaultCharset()); is embarassing me, I think I should go in this way... – ValerianTi Aug 31 '18 at 10:28
  • I've added the picture (actually a link) that shows the encoding in Idea – ValerianTi Aug 31 '18 at 10:32
  • So you restart the ide yet? – yormen Aug 31 '18 at 10:34
  • Tried again right now. It gave me errors about Could not get unknown property 'compileJava' for project '' of type org.gradle.api.Project. - after i deleted this lines it builds. No luck, though, same "??" – ValerianTi Aug 31 '18 at 10:45
1

Well, finally i've got it!

Navigate to help -> edit custom vm options...

enter image description here

Add those ones:

-Dfile.encoding=UTF-8 -Dconsole.encoding=UTF-8

Restart idea completely.

ValerianTi
  • 81
  • 2
  • 11
  • Well, optionally we can add following code in gradlew.bat @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. set DEFAULT_JVM_OPTS=-Dfile.encoding=UTF8 either – ValerianTi Sep 05 '18 at 12:32