How to make sure a String matches a Java class name -
using reflection
, class.forname(string s)
method, there way determine if string matches proper case of class?
example:
class: testreflection.java string s: testreflection try{ class.forname(s) } catch(classnotfoundexception e){ system.out.println("can't find class: " + s); }
this misses classnotfoundexception , throws exception: exception in thread "main" java.lang.noclassdeffounderror: testreflection (wrong name: testreflection)
error log:
exception in thread "main" java.lang.noclassdeffounderror: reflection (wrong name: testreflection) @ java.lang.classloader.defineclass1(native method) @ java.lang.classloader.defineclass(unknown source) @ java.security.secureclassloader.defineclass(unknown source) @ java.net.urlclassloader.defineclass(unknown source) @ java.net.urlclassloader.access$100(unknown source) @ java.net.urlclassloader$1.run(unknown source) @ java.net.urlclassloader$1.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.net.urlclassloader.findclass(unknown source) @ java.lang.classloader.loadclass(unknown source) @ sun.misc.launcher$appclassloader.loadclass(unknown source) @ java.lang.classloader.loadclass(unknown source) @ java.lang.class.forname0(native method) @ java.lang.class.forname(unknown source) @ reflection.findclass(testreflection.java:50) @ reflection.main(testreflection.java:75)
update: changed exception catch throwable
.
i solved problem changing exception type catching classnotfoundexception
throwable
.
Comments
Post a Comment