0

Anyone has idea how to class load a dynamically generated bytecode, which is expected to be in Java system class package (the package name starts with java.lang....).

public class ByteCodeClassLoader extends ClassLoader{
    public static Class<?> run(String className, byte[] b){
         ....
         return load(className, b);
    }
    public   Class<?> load(String className,  byte[] b){
            .............
            Class<?> expClass = null;
            synchronized(this){
                expClass =defineClass(className, b, 0, b.length);
            }
            return expClass;

        return null;
    }

When I try to run the code (the className is java/lang/invoke/DYNGuardWithTestHandle0), it throws exception:

java.lang.NoClassDefFoundError: java/lang/invoke/DYNGuardWithTestHandle0
    at java.lang.ClassLoader.defineClassImpl(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:346)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:283)
    at java.lang.invoke.ByteCodeClassLoader.load(ByteCodeClassLoader.java:83)
    at java.lang.invoke.ByteCodeClassLoader.run(ByteCodeClassLoader.java:54)

Thanks.


The given name is java/lang/invoke/DYNGuardWithTestHandle0, while the class name in the byte[] stream is java.lang.invoke.DYNGuardWithTestHandle0. After making both consistent, the class loading would still fail because the package java.langis protected package.

1 Answer 1

2

Javadoc of defineClass() says:

Throws NoClassDefFoundError if name is not equal to the binary name of the class specified by [byte array]

Sign up to request clarification or add additional context in comments.

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.