6

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

Community
  • 1
  • 1
keighty
  • 362
  • 1
  • 3
  • 11

2 Answers2

4

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.

Community
  • 1
  • 1
Jason Braucht
  • 2,358
  • 19
  • 31
  • Thanks for your thorough answer -- I didn't realize I could create a breakpoint in an imported class! – keighty Apr 20 '12 at 13:15
  • One drawback that I should have mentioned is that you are likely to find breakpoints on methods in `String` or `StringCoding` get tripped a ton (these are frequently used classes). To avoid stepping through every instance, try putting a break in a less frequently used method shortly *before* the method of interest and then when that breakpoint triggers add one to the method you actually want to inspect. Or if your IDE supports it, add a conditional breakpoint so it only triggers on the instances you are interested in. Good luck! – Jason Braucht Apr 20 '12 at 14:07
0

It looks to me as if something in the proprietary API is calling String.getBytes with an empty string for the character set.

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.

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?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • I like the suggestion that it may be looking to run inside a web application container -- unfortunately we licensed the program from a third party, and their unwillingness to support the API suggests they would prefer to do the development themselves. Thanks for your answer -- it is extremely helpful! – keighty Apr 20 '12 at 13:12
  • The problem with your code : "test".getBytes(""); is that you provided an empty String parameter to the "getBytes" method. According to the API it should be a valid Charset, that's why "UnsupportedEncodingException" is thrown. You can either omit this parameter and default OS encoding will be applied or use a valid one. – vs777 Jun 04 '16 at 15:04
  • @vs777: there is no problem with my code, it is supposed to throw an exception. The point of it was to demonstrate how to reproduce the exception message and the first couple of lines of the stacktrace which appears in the question, and it does that successfully, give or take line numbers being slightly out. – Luke Woodward Jun 04 '16 at 18:40