Java 运行时不支持编码“UTF-8”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/177242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:55:26  来源:igfitidea点击:

The encoding 'UTF-8' is not supported by the Java runtime

javalinuxunicode

提问by Mark Derricutt

Whenever I start our Apache Felix (OSGi) based application under SUN Java ( build 1.6.0_10-rc2-b32 and other 1.6.x builds) I see the following message output on the console (usually under Ubuntu 8.4):

每当我在 SUN Java(构建 1.6.0_10-rc2-b32 和其他 1.6.x 构建)下启动基于 Apache Felix (OSGi) 的应用程序时,我都会在控制台上看到以下消息输出(通常在 Ubuntu 8.4 下):

Warning: The encoding 'UTF-8' is not supported by the Java runtime.

警告:Java 运行时不支持编码“UTF-8”。

I've seen this message display occasionally when running both Tomcat and Resin as well. If java supports unicode and UTF-8, what causes this message? I've yet to find any reference, or answer to this anywhere else.

在同时运行 Tomcat 和 Resin 时,我偶尔会看到此消息显示。如果 java 支持 unicode 和 UTF-8,是什么导致了这个消息?我还没有在其他任何地方找到任何参考或答案。

采纳答案by Dan Dyer

According the documentation"Every implementation of the Java platform is required to support the following standard charsets... US-ASCII, ISO-8859-1, UTF-8, UTF-16BE, UTF-16LE, UTF-16." So I doubt that Sun have released a build without UTF-8 support.

根据文档“Java 平台的每个实现都需要支持以下标准字符集... US-ASCII、ISO-8859-1、UTF-8、UTF-16BE、UTF-16LE、UTF-16。” 所以我怀疑 Sun 发布了一个没有 UTF-8 支持的版本。

The actual error message appears to be from here, which is part of the Xerces XML parser. I imagine it is the XML parser where the problem is occurring.

实际的错误消息似乎来自此处,它是 Xerces XML 解析器的一部分。我想这是发生问题的 XML 解析器。

回答by tgdavies

Try the following program:

试试下面的程序:

import java.nio.charset.Charset;

public class TestCharset {
    public static void main(String[] args) {
        System.out.println(Charset.forName("UTF-8"));
    }
}

If this throws an exception, then there is something wrong with your JDK. If it prints "UTF-8" then your JDK is OK, and your application is doing something odd.

如果这引发异常,那么您的 JDK 有问题。如果它打印“UTF-8”,那么您的 JDK 就可以了,并且您的应用程序正在做一些奇怪的事情。

If that's the case, run your app under the debugger, and put a breakpoint in http://www.java2s.com/Open-Source/Java-Document/XML/xalan/org/apache/xml/serializer/ToStream.java.htm--that's the place this warning is produced, and step to see why Xalan can't find the encoding.

如果是这种情况,请在调试器下运行您的应用程序,并在http://www.java2s.com/Open-Source/Java-Document/XML/xalan/org/apache/xml/serializer/ToStream.java 中放置一个断点.htm-- 这是产生此警告的地方,并逐步了解 Xalan 找不到编码的原因。

回答by Vladimir Dyuzhev

Most probably someone put a catch() expecting to have only unsupported encoding exceptions, so he used the appropriate message. But he has used too wide exception specification (e.g. catch( Exception ex ) ), so when at runtime he's got something else (non-valid XML, NPE, ... ) the message became misleading.

很可能有人提出了一个 catch() 期望只有不受支持的编码异常,所以他使用了适当的消息。但是他使用了太广泛的异常规范(例如 catch( Exception ex ) ),所以当在运行时他得到其他东西(无效的 XML、NPE、...)时,该消息变得具有误导性。

回答by Michael Borgwardt

Try a different (stable release) JVM. I had this problem once and it turned out that the machine was running a beta version JVM that indeed did not support UTF-8, contrary to the requirement in the API docs.

尝试不同的(稳定版本)JVM。我曾经遇到过这个问题,结果发现机器正在运行一个测试版的 JVM,它确实不支持 UTF-8,这与 API 文档中的要求相反。

回答by Pavel

It should be "UTF8", without the dash.

它应该是“UTF8”,没有破折号。

回答by Andrew

If you are getting this message when using a Transformer, try to specify the TransformerFactory:

如果您在使用 Transformer 时收到此消息,请尝试指定 TransformerFactory:

link

关联