我们将一个重要的参数读作 vm 参数,它是文件的路径。现在,用户正在使用带有一些韩文字符的 vm 参数(文件夹已用韩文字符命名)并且程序开始崩溃,因为韩文字符被视为问号!下面的实验显示了技术情况。
我试图在 Eclipse 和“VM 参数”的“参数”选项卡下的“调试配置”中调试程序,我给出了这样的输入
-Dfilepath=D:\XXXX\카운터
但是当我从这样的程序中读取它时
String filepath = System.getProperty("filepath");
我得到带有问号的输出,如下所示。
D:\XXXX\???
我知道 eclipse 调试 GUI 使用正确的编码 (?) 来显示正确的字符,但是当在程序中读取该值时,它使用不同的编码,无法正确读取字符。
java 用来读取提供给它的 vm 参数的默认编码是什么?
如何更改eclipse中的编码以便程序正确读取字符?
最佳答案
我的结论是转换取决于默认编码(Windows 设置“非 Unicode 程序的语言”)
下面是测试程序:
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:“非 Unicode 程序的语言”是韩语(韩国)
在命令提示符下执行:
java -Dcenv=카운터 test.Test 카운터
(当我使用 Process Explorer 验证参数时,韩文字符是正确的)Result:
[카운터] sysprop=[카운터], cmd args=[카운터], file.encoding=MS949
测试 2:“非 Unicode 程序的语言”是中文(繁体,台湾)
在命令提示符下执行(从剪贴板粘贴):
java -Dcenv=카운터 test.Test 카운터
(我在命令窗口中看不到韩文字符。但是,当我使用 Process Explorer 验证参数时,韩文字符是正确的)Result:
[카운터] sysprop=[???], cmd args=[???], file.encoding=MS950
测试 3:“非 Unicode 程序的语言”是中文(繁体,台湾)
通过设置程序参数和 VM 参数从 Eclipse 启动(进程资源管理器中的命令行是
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 调试 View 的属性对话框中看到的相同)Result:
[카운터] sysprop=[???], cmd args=[bin], file.encoding=UTF-8
将韩文字符改为“宏碁石”,存在于MS950/MS949字符集中:
[碁石] sysprop=[碁石], cmd args=[碁石], file.encoding=MS949
[碁石] sysprop=[碁石], cmd args=[碁石], file.encoding=MS950
[碁石] sysprop=[碁石], cmd args=[碁石], file.encoding=UTF-8
将韩文字符改为“鈥焢”,存在于MS950字符集中:
[鈥焢] sysprop=[??], cmd args=[??], file.encoding=MS949
[鈥焢] sysprop=[鈥焢], cmd args=[鈥焢], file.encoding=MS950
[鈥焢] sysprop=[鈥焢], cmd args=[鈥焢], file.encoding=UTF-8
将韩文字符改为GBK字符集中存在的“宽广”:
[宽广] sysprop=[??], cmd args=[??], file.encoding=MS949
[宽广] sysprop=[??], cmd args=[??], file.encoding=MS950
[宽广] sysprop=[??], cmd args=[??], file.encoding=UTF-8
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:“非 Unicode 程序的语言”是韩语(韩国)
Process Explorer 中的命令行是
java -Dcenv=移댁슫?? test.Test 移댁슫??
(韩文字符已折叠)Result:
[카운터] sysprop=[移댁슫??], cmd args=[移댁슫??], file.encoding=MS949
测试 6:“非 Unicode 程序的语言”是韩语(韩国)
添加另一个 VM 参数。 Process Explorer 中的命令行是
java -Dfile.encoding=UTF-8 -Dcenv=移댁슫?? test.Test 移댁슫??
(韩文字符已折叠)Result:
[카운터] sysprop=[移댁슫??], cmd args=[移댁슫??], file.encoding=UTF-8
测试7:“非Unicode程序的语言”是中文(繁体,台湾)
Process Explorer 中的命令行是
java -cp s:\ws\wtest\bin -Dcenv=儦渥?? test.Test 儦渥??
(韩文字符已折叠)Result:
[카운터] sysprop=[儦渥??], cmd args=[儦渥??], file.encoding=MS950
关于java - eclipse vm 参数中使用的字符编码是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32587876/