0

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?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • not familiar with that error, but it might be the same as this: https://stackoverflow.com/questions/7509295/noclassdeffounderror-wrong-name – Stultuske Apr 30 '20 at 07:33
  • Yes this is the issue. Is there any workaround? Moving the .class file or making java -cp ... is not a option for me. – Peter Penzov Apr 30 '20 at 07:36

1 Answers1

0

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.

dimo414
  • 47,227
  • 18
  • 148
  • 244