java - 在eclipse中运行带有中文参数的java程序

标签 java eclipse

<分区>

我输入一些参数,比如-D你好=你好,然后调用一个类,

java文件如下:

public class Test {

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

打印结果:

-D??=??

如何解决这个问题?

最佳答案

看起来干净的方式是不可能的: 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.

关于java - 在eclipse中运行带有中文参数的java程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36882559/

相关文章:

java - 如何将复杂的ViewGroup复制到位图?

java - 如何在 Java 中将 MySQL unsigned int 转换为 Protocol Buffer uint32?

java - GWT - JavaScript 调试器的阻力

java - Eclipse 如何知道 com.sun 是受限 API?

java - GCD输入数

android - 执行 doInBackground() 时出错

java - 基因约束似乎没有效果

java - 在 MongoDB 中存储多边形

eclipse - 如何使用 Eclipse Juno Subversive 配置 SVN 存储库?

c++ - eclipse c++ 构建错误 Undefined symbols for architecture x86_64 : ld: symbol(s) not found for architecture x86_64