API 抛出 java.io.UnsupportedEncodingException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10236640/
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
API throws java.io.UnsupportedEncodingException
提问by keighty
I am developing a Java program in eclipse using a proprietary API and it throws the following exception at run-time:
我正在使用专有 API 在 Eclipse 中开发 Java 程序,它在运行时抛出以下异常:
java.io.UnsupportedEncodingException:
at java.lang.StringCoding.encode(StringCoding.java:287)
at java.lang.String.getBytes(String.java:954)...
my code:
我的代码:
private static String SERVER = "localhost";
private static int PORT = 80;
private static String DFT="";
private static String USER = "xx";
private static String pwd = "xx";
public static void main(String[] args) {
LLValue entInfo = new LLValue();
LLSession session = new LLSession(SERVER, PORT, DFT, USER, pwd);
try {
LAPI_DOCUMENTS doc = new LAPI_DOCUMENTS(session);
doc.AccessPersonalWS(entInfo);
} catch (Exception e) {
e.printStackTrace();
}
}
The session appears to open with no errors, but the encoding exception is thrown at doc.AccessEnterpriseWS(entInfo)
会话似乎打开时没有错误,但在 doc.AccessEnterpriseWS(entInfo) 处抛出了编码异常
Through researching this error I have tried using the -encoding option of the compiler, changing the encoding of my editor, etc.
通过研究此错误,我尝试使用编译器的 -encoding 选项、更改编辑器的编码等。
My questions are:
我的问题是:
- how can I find out the encoding of the .class files I am trying to use?
- should I be matching the encoding of my new program to the encoding of the API?
- If java is machine independent why isn't there standard encoding?
- 如何找出我尝试使用的 .class 文件的编码?
- 我应该将新程序的编码与 API 的编码相匹配吗?
- 如果 java 是独立于机器的,为什么没有标准编码?
I have read thisstack trace and thisguide already --
Any suggestions will be appreciated!
任何建议将不胜感激!
Cheers
干杯
采纳答案by Jason Braucht
Run it in your debugger with a breakpoint on String.getBytes()
or StringCoding.encode()
. Both are classes in the JDK so you have access to them and should be able to see what the third party is passing in.
在您的调试器中运行它,断点位于String.getBytes()
或 上StringCoding.encode()
。两者都是 JDK 中的类,因此您可以访问它们并且应该能够看到第三方传入的内容。
The character encodingis used to specify how to interpret the raw binary. The default encoding on English Windows systems in CP1252. Other languages and systems may use different a different default encoding. As a quick test, you might try specifying UTF-8 to see if the problem magically disappears.
该字符编码用于指定如何解释原始二进制。CP1252 英文 Windows 系统上的默认编码。其他语言和系统可能使用不同的不同默认编码。作为快速测试,您可以尝试指定 UTF-8 以查看问题是否神奇地消失。
As noted in this question, the JVM uses the default encoding of the OS, although you can override this default.
如本问题所述,JVM 使用操作系统的默认编码,但您可以覆盖此默认值。
Without knowing more about the third party API you are trying to use, it's hard to say what encoding they might be using. Unfortunately from looking at the implementation of StringCoding.encode()
it appears there are a couple different ways you could get an UnsupportedEncodingException
. Stepping through with a debugger should help narrow things down.
在不了解您尝试使用的第三方 API 的更多信息的情况下,很难说他们可能使用什么编码。不幸的是,从StringCoding.encode()
它的实现来看,似乎有几种不同的方法可以获得UnsupportedEncodingException
. 使用调试器逐步完成应该有助于缩小范围。
回答by Luke Woodward
It looks to me as if something in the proprietary API is calling String.getBytes
with an empty string for the character set.
在我看来,好像专有 API 中的某些东西正在String.getBytes
使用字符集的空字符串调用。
I compiled the following class
我编译了以下类
public class Test2 {
public static void main(String[] args) throws Exception {
"test".getBytes("");
}
}
and when I ran it, I got the following stacktrace:
当我运行它时,我得到了以下堆栈跟踪:
Exception in thread "main" java.io.UnsupportedEncodingException: at java.lang.StringCoding.encode(StringCoding.java:286) at java.lang.String.getBytes(String.java:954) at Test2.main(Test2.java:3)
I would be surprised if this is anything to do with the encoding in which the class files are written. It looks to me like this is a problem with code, not a problem you can fix by changing file encodings or compiler/JVM switches.
如果这与编写类文件的编码有关,我会感到惊讶。在我看来,这是代码问题,而不是您可以通过更改文件编码或编译器/JVM 开关来解决的问题。
I don't know anything about what this proprietary API is supposed to do or how it works. Perhaps it is expecting to be run inside a Java EE or web application container? Perhaps it has a bug? Perhaps it needs more configuration before it can run without throwing exceptions? Given that it's proprietary, can you get any support from the vendor?
我对这个专有 API 应该做什么或它如何工作一无所知。也许它希望在 Java EE 或 Web 应用程序容器中运行?也许它有一个错误?也许它需要更多配置才能运行而不抛出异常?鉴于它是专有的,你能从供应商那里得到任何支持吗?