dimanche 25 décembre 2016

JAVA - How to get package distance when Initial Class Loader

I have class file from cloud network, for sample test on my project.

I want test this file using reflection. so I must initial class using class loader.

for e.g

File file = new File("path");
URL[] cp = 
{
    file.getParentFile().toURI().toURL()
};
URLClassLoader urlcl = new URLClassLoader(cp);
Class cls = urlcl.loadClass(file.getName().replaceAll("\\.class", ""));

so I get an error because I don't know package that included class like com.mypackage:

Exception in thread "main" java.lang.NoClassDefFoundError: packclass (wrongname: dirclass\packclass)

so, my question how can I know package that included to my class file.





Aucun commentaire:

Enregistrer un commentaire