eclipse vm 参数中使用的字符编码是什么?

what is the character encoding used in eclipse vm arguement?

我们读取了一个重要的参数作为vm参数,它是一个文件的路径。现在,用户正在使用带有一些韩文字符的 vm 参数(文件夹已用韩文字符命名)并且程序开始中断,因为韩文字符被读取为问号!下面的实验显示了技术情况。

我尝试在 "VM arguments" 中的 "arguments" 选项卡下的 "Debug Configurations" 和 "Debug Configurations" 中调试程序,我给出了这样的输入

-Dfilepath=D:\XXXX\카운터

但是当我像这样从程序中读取它时

String filepath = System.getProperty("filepath");

我得到如下带问号的输出。

D:\XXXX\???

我知道 eclipse 调试 GUI 使用正确的编码 (?) 来显示正确的字符,但是当在程序中读取值时它使用不同的编码无法正确读取字符。

java 使用什么默认编码来读取提供给它的 vm 参数?

如何更改eclipse中的编码,使程序正确读取字符?

我的结论是转换取决于默认编码(Windows设置"Language for non-Unicode programs") 这是测试程序:

package test;
import java.io.FileOutputStream;
public class Test {
public static void main(String[] args) throws Exception {
    StringBuilder sb = new StringBuilder();
    sb.append("[카운터] sysprop=[").append(System.getProperty("cenv"));
    if (args.length > 0) {
        sb.append("], cmd args=[").append(args[0]);
    }
    sb.append("], file.encoding=").append(System.getProperty("file.encoding"));
    FileOutputStream fout = new FileOutputStream("/testout");
    fout.write(sb.toString().getBytes("UTF-8"));
    fout.close();//write result to a file instead of System.out
    //Thread.sleep(10000);//For checking arguments using Process Explorer
}
}

测试 1:"Language for non-Unicode programs" 是韩国人(韩国)

在命令提示符下执行:java -Dcenv=카운터 test.Test 카운터(当我使用 Process Explorer 验证参数时,韩文字符是正确的)

Result: [카운터] sysprop=[카운터], cmd args=[카운터], file.encoding=MS949

测试2:"Language for non-Unicode programs"是中文(繁体,台湾)

在命令提示符下执行(从剪贴板粘贴):java -Dcenv=카운터 test.Test 카운터(我在命令 windows 中看不到韩语字符。但是,当我使用 Process Explorer 验证参数时,韩语字符是正确的)

Result: [카운터] sysprop=[???], cmd args=[???], file.encoding=MS950

测试 3:"Language for non-Unicode programs" 是中文(繁体,台湾)

通过设置程序参数和 VM 参数从 Eclipse 启动(Process Explorer 中的命令行是 C:\pg\jdk160\bin\javaw.exe -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:50672 -Dcenv=카운터 -Dfile.encoding=UTF-8 -classpath S:\ws\wtest\bin test.Test 카운터 这与您在 Eclipse 调试视图的属性对话框中看到的相同)

Result: [카운터] sysprop=[???], cmd args=[bin], file.encoding=UTF-8

将韩文字符更改为MS950/MS949字符集中存在的“碁石”:

  • 测试 1 结果:[碁石] sysprop=[碁石], cmd args=[碁石], file.encoding=MS949
  • 测试 2 结果:[碁石] sysprop=[碁石], cmd args=[碁石], file.encoding=MS950
  • 测试 3 结果:[碁石] sysprop=[碁石], cmd args=[碁石], file.encoding=UTF-8

将韩文字符更改为MS950字符集中存在的“鈥焢”:

  • 测试 1 结果:[鈥焢] sysprop=[??], cmd args=[??], file.encoding=MS949
  • 测试 2 结果:[鈥焢] sysprop=[鈥焢], cmd args=[鈥焢], file.encoding=MS950
  • 测试 3 结果:[鈥焢] sysprop=[鈥焢], cmd args=[鈥焢], file.encoding=UTF-8

将韩文字符改为GBK字符集中存在的“宽广”:

  • 测试 1 结果:[宽广] sysprop=[??], cmd args=[??], file.encoding=MS949
  • 测试 2 结果:[宽广] sysprop=[??], cmd args=[??], file.encoding=MS950
  • 测试 3 结果:[宽广] sysprop=[??], cmd args=[??], file.encoding=UTF-8
  • Test4:为了验证我的假设,我将 "Language for non-Unicode programs" 更改为 Chinese(Simplified, PRC) 并在命令提示符下执行 java -Dcenv=宽广 test.Test 宽广

    Result: [宽广] sysprop=[宽广], cmd args=[宽广], file.encoding=GBK

在测试期间,我总是通过 Process Explorer 检查命令行,并确保所有字符都是正确的。 但是,命令参数字符在调用 main(String[] args) of Java class 之前使用默认编码进行转换。如果默认编码的字符集中不存在其中一个字符,程序将得到意外的参数。

我不确定问题是由 java.exe/javaw.exe 或 Windows 引起的。但是通过命令参数传递非 ASCII 参数不是一个好主意。

顺便说一句,我也尝试通过.bat文件(文件编码为UTF-8)执行命令。也许有人感兴趣,

测试 5:"Language for non-Unicode programs" 是韩国人(韩国)

Process Explorer 中的命令行是java -Dcenv=移댁슫?? test.Test 移댁슫??(韩文字符已折叠)

Result: [카운터] sysprop=[移댁슫??], cmd args=[移댁슫??], file.encoding=MS949

测试 6:"Language for non-Unicode programs" 是韩国人(韩国)

添加另一个 VM 参数。 Process Explorer 中的命令行是 java -Dfile.encoding=UTF-8 -Dcenv=移댁슫?? test.Test 移댁슫??(韩文字符已折叠)

Result: [카운터] sysprop=[移댁슫??], cmd args=[移댁슫??], file.encoding=UTF-8

测试7:"Language for non-Unicode programs"是中文(繁体,台湾)

Process Explorer 中的命令行是java -cp s:\ws\wtest\bin -Dcenv=儦渥?? test.Test 儦渥??(韩文字符已折叠)

Result: [카운터] sysprop=[儦渥??], cmd args=[儦渥??], file.encoding=MS950