Quick fix for Class.forName case issue [duplicate]

情到浓时终转凉″ 提交于 2020-01-11 07:40:13

问题


I get this really (stupid) error from Java

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: models/modelclass (wrong name: models/ModelClass)

So I am typing in a command at the command line, and I would rather not type the proper case of the class name. I'd like to type "modelclass" instead of "ModelClass".

Is there a way to solve this? Why does this exception exist?!?


回答1:


The error exists because the standard Java classloaders are case-sensitive to class names.

Three options:

  1. Ignore standard Java conventions and name all your classes lowercase (not recommended, and not possible if you are looking for a third-party class).
  2. Use Google's Reflections Library to look up classes in your classpath, do a case insensitive match against the given input, and use the class you find from reflection in your Class.forName() call.
  3. Iteration on #2: Write your own classloader that does a case-insensitive search for classes and loads the one you want.


来源:https://stackoverflow.com/questions/19281781/quick-fix-for-class-forname-case-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!