本文介绍了快速修复Class.forName大小写问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Java中得到这个真正的(愚蠢的)错误

 线程中的异常AWT-EventQueue-0java.lang .NoClassDefFoundError:models / modelclass(错误的名称:models / ModelClass)

在命令行,我宁愿不输入类名的正确case。我想输入modelclass而不是ModelClass。



有没有办法解决这个问题?为什么存在这个异常?

解决方案

错误存在,因为标准的Java类加载器对类名区分大小写。 / p>

三个选项:


  1. 忽略标准Java约定,并将所有类命名为小写不推荐,如果您正在寻找第三方类,则不可能。)

  2. 使用在类路径中查找类,对给定的输入执行不区分大小写的匹配,并使用您在Class.forName()调用中从反射中找到的类。 li>
  3. 在#2上迭代:编写自己的类加载器,对类进行不区分大小写的搜索,并加载所需的类。


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?!?

解决方案

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.

这篇关于快速修复Class.forName大小写问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:53