Setting the default Java character encoding

后端 未结 17 1727
终归单人心
终归单人心 2020-11-21 06:09

How do I properly set the default character encoding used by the JVM (1.5.x) programmatically?

I have read that -Dfile.encoding=whatever used to be the

相关标签:
17条回答
  • 2020-11-21 07:02

    I'm using Amazon (AWS) Elastic Beanstalk and successfully changed it to UTF-8.

    In Elastic Beanstalk, go to Configuration > Software, "Environment properties". Add (name) JAVA_TOOL_OPTIONS with (value) -Dfile.encoding=UTF8

    After saving, the environment will restart with the UTF-8 encoding.

    0 讨论(0)
  • 2020-11-21 07:02

    Solve this problem in my project. Hope it helps someone.

    I use LIBGDX java framework and also had this issue in my android studio project. In Mac OS encoding is correct, but in Windows 10 special characters and symbols and also russian characters show as questions like: ????? and other incorrect symbols.

    1. Change in android studio project settings: File->Settings...->Editor-> File Encodings to UTF-8 in all three fields (Global Encoding, Project Encoding and Default below).

    2. In any java file set:

      System.setProperty("file.encoding","UTF-8");

    3. And for test print debug log:

      System.out.println("My project encoding is : "+ Charset.defaultCharset());

    0 讨论(0)
  • 2020-11-21 07:04

    From the JVM™ Tool Interface documentation…

    Since the command-line cannot always be accessed or modified, for example in embedded VMs or simply VMs launched deep within scripts, a JAVA_TOOL_OPTIONS variable is provided so that agents may be launched in these cases.

    By setting the (Windows) environment variable JAVA_TOOL_OPTIONS to -Dfile.encoding=UTF8, the (Java) System property will be set automatically every time a JVM is started. You will know that the parameter has been picked up because the following message will be posted to System.err:

    Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8

    0 讨论(0)
  • 2020-11-21 07:05

    We set there two system properties together and it makes the system take everything into utf8

    file.encoding=UTF8
    client.encoding.override=UTF-8
    
    0 讨论(0)
  • 2020-11-21 07:05

    My team encountered the same issue in machines with Windows.. then managed to resolve it in two ways:

    a) Set enviroment variable (even in Windows system preferences)

    JAVA_TOOL_OPTIONS
    -Dfile.encoding=UTF8

    b) Introduce following snippet to your pom.xml:

     -Dfile.encoding=UTF-8 
    

    WITHIN

     <jvmArguments>
     -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8001
     -Dfile.encoding=UTF-8
     </jvmArguments>
    
    0 讨论(0)
提交回复
热议问题