简体   繁体   中英

Java classloader Can not call newInstance() on the Class for java.lang.Class

I am trying to write a class loader from a byte stream, but I get this error. I've read the documentation and I am confused as this shouldn't be happening. Here is what I am trying to load:

class LoadedClass {
    public void out(String msg) {
        System.out.println(msg);
    }
}

which is stored in a byte array

And here is my classloader code:

package byteclassloader;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class PrintClass extends ClassLoader {
    private String LoadedClass = "cafebabe00000033001e0a0005001309001400150a001600170700180700190100063c696e69743e010003282956010004436f646501000f4c696e654e756d6265725461626c650100124c6f63616c5661726961626c655461626c650100047468697301000d4c4c6f61646564436c6173733b0100036f7574010015284c6a6176612f6c616e672f537472696e673b29560100036d73670100124c6a6176612f6c616e672f537472696e673b01000a536f7572636546696c650100104c6f61646564436c6173732e6a6176610c0006000707001a0c000d001b07001c0c001d000e01000b4c6f61646564436c6173730100106a6176612f6c616e672f4f626a6563740100106a6176612f6c616e672f53797374656d0100154c6a6176612f696f2f5072696e7453747265616d3b0100136a6176612f696f2f5072696e7453747265616d0100077072696e746c6e002000040005000000000002000000060007000100080000002f00010001000000052ab70001b100000002000900000006000100000001000a0000000c000100000005000b000c00000001000d000e00010008000000400002000200000008b200022bb60003b10000000200090000000a00020000000300070004000a00000016000200000008000b000c000000000008000f0010000100010011000000020012";
    private String name = "LoadedClass";

    public Class findClass() {
        byte[] ba = hexStringToByteArray(LoadedClass);
        return defineClass(name, ba, 0, ba.length);
    }

    private static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte data[] = new byte[len / 2];

        for( int i = 0; i < len; i+= 2 ) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                   + Character.digit(s.charAt(i+1), 16));
        }

        return data;
    }
}

public class ByteClassLoader {


    public static void main(String[] args) {
        try {
            Class LoadedClass = new PrintClass().findClass().getClass();
            Object inst = LoadedClass.newInstance();
            String out = "The quick brown fox jumps over the gluttonous sloth sow.";
            Method printStr = LoadedClass.getMethod("out", new Class[] { String.class });
            printStr.invoke(inst, out);
        }
        catch( InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e ) {
            System.out.println(e.getMessage());
        }
    }


}

It throws this exception:

Can not call newInstance() on the Class for java.lang.Class

Class loadedClass = new PrintClass().findClass();

Do not do getClass() of findClass() as it will return java.lang.Class

Now after getting the class loaded you can create the instance:

Object inst = loadedClass.newInstance(); //if the default constructor is present

findClass() already returns a Class instance of your loaded class. That's what you should be calling newInstance() on. Otherwise

new PrintClass().findClass().getClass();

returns and instance of type Class<Class> and since Class has a private empty constructor. It is not accessible.

/*
 * Constructor. Only the Java Virtual Machine creates Class
 * objects.
 */
private Class() {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM