samedi 28 décembre 2019

Java access Methods from .class files

Hey I'm trying to use javas reflection capabilities to get some info on methods declared in an unknown class file (Decoder.class)

I first placed the Decoder.class file in the root directory of my (eclipse) project workspace.

try {
        File file = new File(".");
        URL url = file.toURL();
        URL[] urls = new URL[] { url };
        ClassLoader cl = new URLClassLoader(urls);
        Class c = cl.loadClass("Decoder");
        System.out.println(c.isInterface());

        printMethods(c);
        printConstructor(c);
    }
    catch (ClassNotFoundException e) {
        System.out.println("Requested class was not found " + e.getMessage());
    }
    catch (MalformedURLException e) {
        System.out.println("Given class file url was not found " + e.getMessage());
    }

that gave me the error

Exception in thread "main" java.lang.NoClassDefFoundError: Decoder (wrong name: ea_6_1/Decoder)

That looks to my that the name of the unkown class within Decoder.class is actually Decoder. However it's declared within the package "ea_6_1")

i then created a subfolder ea_6_1 within the workspace and changed

 Class c = cl.loadClass("Decoder");

to

Class c = cl.loadClass("ea_6_1/Decoder");

but this gives me an illegal name exception.

Exception in thread "main" java.lang.NoClassDefFoundError: IllegalName: ea_6_1/Decoder

So how can i Import a class from another package?

greets:)





Aucun commentaire:

Enregistrer un commentaire