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:)
使用完全限定的类名Class c = cl.loadClass("com.example.ea_6_1.Decoder");
Try using the fully qualified class name:
try {
File file = new File(".");
URL url = file.toURL();
URL[] urls = new URL[] { url };
ClassLoader cl = new URLClassLoader(urls);
Class c = cl.loadClass("design.pattern.Person");
System.out.println(c.isInterface());
}
catch (Exception e) {
e.printStackTrace();
}
You need to give the binary name /fully qualified name as specified in the docs .
So instead of using Example
you need to use full.package.name.Example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.