如何确保String与Java类名匹配



使用reflectionClass.forName(String s)方法,有没有办法确定我的String是否与类的正确大小写匹配?

示例:

Class: testReflection.java
String s: testReFlEction
try{   
    Class.forName(s)
}
    catch(ClassNotFoundException e){
    System.out.println("Can't find class: " + s);
}

这会错过ClassNotFoundException并引发此异常:线程"main"java.lang.NoClassDefFoundError:testReFlEction(错误名称:testReFlEction)中的异常

错误日志:

Exception in thread "main" java.lang.NoClassDefFoundError: ReFlEction (wrong name:  testReflection)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Reflection.findClass(testReflection.java:50)
at Reflection.main(testReflection.java:75)

更新:我将异常更改为捕获Throwable

我通过将捕获的异常类型从ClassNotFoundException更改为Throwable来解决问题。

相关内容

  • 没有找到相关文章

最新更新