Wrong name error when class loaded from random dir

101 Views Asked by At

I want to make a instance of .class file located into random directory. I tried this

private final String CLASS_FOLDER =
            "C:\\Users\\test\\Desktop\\fix\\core\\src\\test\\org\\poc\\";

    private  Class getClassFromFile(String fullClassName) throws Exception {
        URLClassLoader loader = new URLClassLoader(new URL[] {
                new URL("file://" + CLASS_FOLDER)
        });
        return loader.loadClass("Order");
    }

When I run the code I get error:

java.lang.NoClassDefFoundError: Order (wrong name: com/solutions/backend/toms/actions/Order)

Looks like a security check for correct package name. Is there nay way to skip this check because I need to load .class files into random directories?

1

There are 1 best solutions below

0
dimo414 On

Java classes need to be in a directory hierarchy that matches their package. You cannot drop a Java .class in a "random" directory, that's simply not how classloading works.