1

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

1
  • Jason I guess you want to create the instance of the class loaded using the classloader. Right? Then you should change the title of the question. Commented Aug 30, 2013 at 20:01

2 Answers 2

5
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
Sign up to request clarification or add additional context in comments.

1 Comment

@ErnestFriedman-Hill why is that irrelevant? IMO OP wants to create the instance of the class that is loaded using the classloader.
3

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() {}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.