使用 -Dfile.encoding=UTF-8 启动 java 应用程序时,System.getProperty("non-english.path") 返回错误的字符集字符

问题描述 投票:0回答:1

在 Windows 10 上,我将默认字符从

UTF-8
设置为
Control Pane > Clock And Region > Region > Administrative > Change System locale
,选中
Beta: Use Unicode UTF-8 for worldwide language support
并重新启动机器。

使用以下命令启动java(jdk1.8.0_212)应用程序(

中区
是一些汉字):

java -Dfile.encoding=UTF-8 -Dv8.path="D:\\中区path" App

System.getProperty("v8.path")
的值是一个很大的
D:\涓尯path
而不是
D:\\中区path
,从而导致应用程序因
NoSuchFileException
而崩溃。

如何让

System.getProperty
返回正确的字符?


更新:

  • 我使用 Windows cmd,并运行
    chcp
    打印:
    Active code page: 65001
    
  • System.getProperty("v8.path").toCharArray()
    的字符值:
    'D' 68
    ':' 58
    '\\' 92
    '\\' 92
    '涓' 28051
    '\uE15E' 57694
    '尯' 23599
    'p' 112
    'a' 97
    't' 116
    'h' 104
    
java encoding utf-8
1个回答
0
投票

约瑟夫兹是对的。给出以下 java 代码:

Charset charset = Charset.forName("gb18030");
ByteBuffer buffer = charset.encode(CharBuffer.wrap(System.getProperty("v8.path")));

CharBuffer decode = StandardCharsets.UTF_8.decode(buffer);
System.out.println(decode.toString());  

结果就是期望值:

D:\中区path
© www.soinside.com 2019 - 2024. All rights reserved.