Java8 FileSystems.getDefault 抛出 UnsupportedCharsetException
Java8 FileSystems.getDefault throw UnsupportedCharsetException
我有一个非常简单的代码:
public static void main(String[] arg){
FileSystems.getDefault();
}
它在 Java7
中运行良好
但抛出 UnsupportedCharsetException: GBK in java8,完整堆栈如下:
Exception in thread "main" java.lang.ExceptionInInitializerError
at sun.nio.fs.WindowsFileSystem.<init>(WindowsFileSystem.java:67)
at sun.nio.fs.WindowsFileSystemProvider.<init>(WindowsFileSystemProvider.java:53)
at sun.nio.fs.DefaultFileSystemProvider.create(DefaultFileSystemProvider.java:36)
at java.nio.file.FileSystems$DefaultFileSystemHolder.getDefaultProvider(FileSystems.java:108)
at java.nio.file.FileSystems$DefaultFileSystemHolder.access[=13=]0(FileSystems.java:89)
at java.nio.file.FileSystems$DefaultFileSystemHolder.run(FileSystems.java:98)
at java.nio.file.FileSystems$DefaultFileSystemHolder.run(FileSystems.java:96)
at java.security.AccessController.doPrivileged(Native Method)
at java.nio.file.FileSystems$DefaultFileSystemHolder.defaultFileSystem(FileSystems.java:96)
at java.nio.file.FileSystems$DefaultFileSystemHolder.<clinit>(FileSystems.java:90)
at java.nio.file.FileSystems.getDefault(FileSystems.java:176)
at com.yy.ent.test.Test1.main(Test1.java:26)
Caused by: java.nio.charset.UnsupportedCharsetException: GBK
at java.nio.charset.Charset.forName(Charset.java:531)
at sun.nio.fs.Util.<clinit>(Util.java:41)
... 12 more
我用的是中文windows7
而我的 java8 是:
- Java(TM) SE 运行时环境(build 1.8.0_66-b18)
- Java HotSpot(TM) 64 位服务器 VM(构建 25.66-b18,混合模式)
我该如何解决这个问题?
要么尝试将 "sun.jnu.encoding" 属性 更改为其他内容,例如:
-Dsun.jnu.encoding=UTF-8
或根据 (http://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html)
检查您的 JVM 是否包含支持 GBK 所需的 "lib/charsets.jar"
如果语言环境需要,JRE 安装程序似乎只安装 "extended" 个字符集:https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html
Oracle's Java SE Development Kit 7 (Java SE 7) for all platforms (Solaris, Linux, and Microsoft Windows) and the Java SE Runtime Environment 7 (JRE 7) for Solaris and Linux support all encodings shown on this page. Oracle's JRE 7 for Microsoft Windows may be installed as a complete international version or as a European languages version. By default, the JRE 7 installer installs a European languages version if it recognizes that the host operating system only supports European languages. If the installer recognizes that any other language is needed, or if the user requests support for non-European languages in a customized installation, a complete international version is installed. The European languages version only supports the encodings shown in the following Basic Encoding Set table. The international version (which includes the lib/charsets.jar file) supports all encodings shown on this page.
这应该也适用于 Java 8。我的意思是,JDK 支持所有字符集(包括 lib/charsets.jar
)。 JRE 会尝试安装最小环境,并且不会安装扩展字符集,例如 GBK,如果安装程序由具有欧洲区域设置的用户 运行。
解法:
尝试安装 JDK 或将用户的区域和语言设置为中文(简体),然后重新安装 JRE。
我有一个非常简单的代码:
public static void main(String[] arg){
FileSystems.getDefault();
}
它在 Java7
中运行良好但抛出 UnsupportedCharsetException: GBK in java8,完整堆栈如下:
Exception in thread "main" java.lang.ExceptionInInitializerError
at sun.nio.fs.WindowsFileSystem.<init>(WindowsFileSystem.java:67)
at sun.nio.fs.WindowsFileSystemProvider.<init>(WindowsFileSystemProvider.java:53)
at sun.nio.fs.DefaultFileSystemProvider.create(DefaultFileSystemProvider.java:36)
at java.nio.file.FileSystems$DefaultFileSystemHolder.getDefaultProvider(FileSystems.java:108)
at java.nio.file.FileSystems$DefaultFileSystemHolder.access[=13=]0(FileSystems.java:89)
at java.nio.file.FileSystems$DefaultFileSystemHolder.run(FileSystems.java:98)
at java.nio.file.FileSystems$DefaultFileSystemHolder.run(FileSystems.java:96)
at java.security.AccessController.doPrivileged(Native Method)
at java.nio.file.FileSystems$DefaultFileSystemHolder.defaultFileSystem(FileSystems.java:96)
at java.nio.file.FileSystems$DefaultFileSystemHolder.<clinit>(FileSystems.java:90)
at java.nio.file.FileSystems.getDefault(FileSystems.java:176)
at com.yy.ent.test.Test1.main(Test1.java:26)
Caused by: java.nio.charset.UnsupportedCharsetException: GBK
at java.nio.charset.Charset.forName(Charset.java:531)
at sun.nio.fs.Util.<clinit>(Util.java:41)
... 12 more
我用的是中文windows7 而我的 java8 是:
- Java(TM) SE 运行时环境(build 1.8.0_66-b18)
- Java HotSpot(TM) 64 位服务器 VM(构建 25.66-b18,混合模式)
我该如何解决这个问题?
要么尝试将 "sun.jnu.encoding" 属性 更改为其他内容,例如:
-Dsun.jnu.encoding=UTF-8
或根据 (http://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html)
检查您的 JVM 是否包含支持 GBK 所需的 "lib/charsets.jar"如果语言环境需要,JRE 安装程序似乎只安装 "extended" 个字符集:https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html
Oracle's Java SE Development Kit 7 (Java SE 7) for all platforms (Solaris, Linux, and Microsoft Windows) and the Java SE Runtime Environment 7 (JRE 7) for Solaris and Linux support all encodings shown on this page. Oracle's JRE 7 for Microsoft Windows may be installed as a complete international version or as a European languages version. By default, the JRE 7 installer installs a European languages version if it recognizes that the host operating system only supports European languages. If the installer recognizes that any other language is needed, or if the user requests support for non-European languages in a customized installation, a complete international version is installed. The European languages version only supports the encodings shown in the following Basic Encoding Set table. The international version (which includes the lib/charsets.jar file) supports all encodings shown on this page.
这应该也适用于 Java 8。我的意思是,JDK 支持所有字符集(包括 lib/charsets.jar
)。 JRE 会尝试安装最小环境,并且不会安装扩展字符集,例如 GBK,如果安装程序由具有欧洲区域设置的用户 运行。
解法: 尝试安装 JDK 或将用户的区域和语言设置为中文(简体),然后重新安装 JRE。