Japanese string in Java

I am new to Java, so please bear with me if this is a very simple problem. I have a JUnit Test, where I have a hard-coded Japanese word assigned directly to a string variable. Now, right after this line is assigned, it turns into "??" that some some some encoding.

public class TestTest extends TestCase {
  public void testLocal(){
    Locale.setDefault(Locale.JAPAN);//same problem with or without this line
    String test = "会社";
    //after this line, by watching at the debugger, the variable "test" contains "??"
    assertEquals("会社", test);
  }
}

Since this is a test file, I believe that it completely isolates the problem from other user interfaces. Please help me with this. It was 2 days without a decision. Thank you in advance.

+3
source share
4 answers

, , ... escape- \uxxxx, , , , .

, , ( ..) . IDE, Ant .. (, -encoding javac).

+4

: . , , LANG.

, ( ) , , . (Bash):

export LANG="ja_JP.UTF-8"

Windows:

set LANG=ja_JP.UTF-8

, : chsh 65001, java-. , ... , !

+1

System.out, , .

-Dfile.encoding = UTF8, ( !)

. : java -Dfile.encoding = UTF8 MyApp

(. CLI, )

+1

    ...
    assertEquals("会社", new String(test.getBytes(),"utf8"));
    ...
+1

Source: https://habr.com/ru/post/1718769/


All Articles