Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API throws java.io.UnsupportedEncodingException

Tags:

java

I am developing a Java program in eclipse using a proprietary API and it throws the following exception at run-time:

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)

Through researching this error I have tried using the -encoding option of the compiler, changing the encoding of my editor, etc.

My questions are:

  1. how can I find out the encoding of the .class files I am trying to use?
  2. should I be matching the encoding of my new program to the encoding of the API?
  3. If java is machine independent why isn't there standard encoding?

I have read this stack trace and this guide already --

Any suggestions will be appreciated!

Cheers

like image 789
keighty Avatar asked Apr 19 '12 20:04

keighty


1 Answers

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.

The character encoding is 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.

As noted in this question, the JVM uses the default encoding of the OS, although you can override this default.

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.

like image 94
Jason Braucht Avatar answered Sep 28 '22 00:09

Jason Braucht