4

I input some arguments likes -D你好=你好, and invoke a single class ,

the java file as follows:

public class Test {

public static void main(String[] args) throws Exception {
    for(String name :args){
        System.out.println(name);
    }
}

}

print results:

-D??=??

how could fix this issue?

Davy
  • 41
  • 1
  • See if those two links help : http://www.mkyong.com/java/how-to-display-chinese-character-in-eclipse-console/ and http://stackoverflow.com/questions/8056900/how-to-print-simplified-chinese-characters-to-eclipse-console – Arnaud Apr 27 '16 at 07:03
  • Have you tryed this? [How to print [Simplified] Chinese characters to Eclipse console?](http://stackoverflow.com/questions/8056900/how-to-print-simplified-chinese-characters-to-eclipse-console) or [How to support UTF-8 encoding in Eclipse](http://stackoverflow.com/questions/9180981/how-to-support-utf-8-encoding-in-eclipse) – Julio M Apr 27 '16 at 07:33
  • The console could show correct Chinese characters and project , java file, common encoding in debug configurations all seted UTF-8 , but strange , why setting chinese character in program arguments view, the main method get the args couldn't show correct. – Davy Apr 27 '16 at 07:54

1 Answers1

2

Looks like a clean way is not possible : Passing command line unicode argument to Java code

Unfortunately you cannot reliably use non-ASCII characters with command-line apps that use the Windows C runtime's stdlib, like Java (and pretty much all non-Windows-specific scripting languages really).

This is because they read their input and output using a locale-specific code page by default, which is never a UTF, unlike every other modern OS which uses UTF-8.

Whilst you can change the code page of a terminal to something else using the chcp command, the support for the UTF-8 encoding under chcp 65001 is broken in a few ways that are likely to trip apps up fatally.

If you only need Japanese you could switch to code page 932 (similar to Shift-JIS) by setting your locale (‘language for non-Unicode applications’ in the Regional settings) to Japan. This will still fail for characters that aren't in that code page though.

If you need to get non-ASCII characters through the command line reliably on Windows, you need to call the Win32 API function GetCommandLineW directly to avoid the encode-to-system-code-page layer. Probably you'd want to do that using JNA.

Community
  • 1
  • 1
flavio.donze
  • 7,432
  • 9
  • 58
  • 91