0

Using reflection and the Class.forName(String s) method, is there a way to determine if my String matches the proper case of the 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 the ClassNotFoundException and throws this 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)
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)

Update: I changed the exception to catch Throwable.

Yehuda Gutstein
  • 381
  • 10
  • 27
  • possible duplicate of [Quick fix for Class.forName case issue](http://stackoverflow.com/questions/19281781/quick-fix-for-class-forname-case-issue) – Jeroen Vannevel Apr 20 '14 at 21:38
  • Thanks, I missed that before writing the question. I will look into it and see if it solves my question. – Yehuda Gutstein Apr 20 '14 at 21:39
  • Sidenote: the duplicate on that question is not a duplicate at all, I don't know why people closed it as such. The gist is this: you'll have to either use an external library or write your own classloader. There is no easy solution. If you however stick to naming conventions, as you should, you can solve it very easily by transforming the input into a string representing the convention. – Jeroen Vannevel Apr 20 '14 at 21:40
  • @ Jeroen Vannevel, He says to do a case insensitive search. I was trying to do that beforehand, which is how I came to my question. However, I can't seem to figure out how to check the actual classname with case insensitivity? – Yehuda Gutstein Apr 20 '14 at 21:44
  • That advise goes with the google library which I assume contains a way to do this search case-insensitively. Did you use that library as well? – Jeroen Vannevel Apr 20 '14 at 21:51
  • no, I am using straight up java, and am kinda lost – Yehuda Gutstein Apr 20 '14 at 21:52

1 Answers1

0

I solved my problem by changing the exception type I am catching from ClassNotFoundException to Throwable.

Yehuda Gutstein
  • 381
  • 10
  • 27