Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassFormatError: Unknown constant tag in class XY

Tags:

java

I have a piece of code in which I try to load a class during runtime. The code is not all selfwritten so I have some problems understanding the error which always appers after compiling.

Here the code:

private Class findClass(String s)
        throws ClassNotFoundException
    {
        URL url = getResource("AP.class");
        if(url == null)
        {
            throw new ClassNotFoundException(s);
        }
        inputstream = null;
        Class classToRead;
        try
        {
            inputstream = url.openStream();
            byte abyte0[] = readClass(inputstream);
            classToRead= defineClass(s, abyte0, 0, abyte0.length);
        }
        catch(IOException ioexception)
        {
            throw new ClassNotFoundException(s);
        }
        if(inputstream != null)
        {
            try
            {
                inputstream.close();
            }
            catch(Exception exception1) { }
        }
        return classToRead;
    }

The error appears at the defineClass method.

Error stacktrace:

Exception in thread "main" java.lang.ClassFormatError: Unknown constant tag 63 in class file AP
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at c.findClass(c.java:100)
    at c.loadClass(c.java:56)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at c.a(c.java:20)
    at mainOpenClass.main(lol.java:13)

My questions now are: Is maybe anything wrong with the code? Is it possible that the AP.class is damaged? What does the error really mean?

I hope anybody can help me with my problem because searching the internet didn't really help in that case.

like image 358
Mister004 Avatar asked Sep 08 '12 19:09

Mister004


1 Answers

The code you've written yourself is fine. The problem is that AP.class is a damaged class file -- hence the ClassFormatError.

The error itself means that it failed to correctly decode the constant pool, a section of the class file structure that is much like a symbol table. See §4.4 of the Java Virtual Machine Specification:

Java virtual machine instructions do not rely on the runtime layout of classes, interfaces, class instances, or arrays. Instead, instructions refer to symbolic information in the constant_pool table.

All constant_pool table entries have the following general format:

cp_info {
    u1 tag;
    u1 info[];
}

Each item in the constant_pool table must begin with a 1-byte tag indicating the kind of cp_info entry. The contents of the info array vary with the value of tag. The valid tags and their values are listed in Table 4.3. Each tag byte must be followed by two or more bytes giving information about the specific constant. The format of the additional information varies with the tag value.

So, the error itself is telling you that the class has a constant pool table entry with an invalid tag, namely 63. Verifying with Table 4.3 mentioned above, indeed, this does not correspond to any documented kind of cp_info entry.

Try to re-download AP.class. Given the obscure names (AP, as well as c from the stack trace), I'm going to assume you're trying to use some obfuscated code. Verify not only that the code you're trying to deal with is not further protected by some sort of encryption, but also that any preprocessing you do (e.g. deobfuscation) does not corrupt the data.

like image 101
obataku Avatar answered Sep 22 '22 11:09

obataku