0

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

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

4

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.
lreeder
  • 12,047
  • 2
  • 56
  • 65
  • 2
    Don't encourage the behavior :-) – Jim Garrison Oct 09 '13 at 20:27
  • 1
    I think there's a valid use case for case-insensitive searches for class names (I like it when my IDE does it), but you're right that skipping the Java class naming conventions is not a good idea. – lreeder Oct 09 '13 at 20:32
  • well, also, my question is, why are the standard Java classloaders case-sensitive to class names? There may be a good reason, and I am just curious. – Alexander Mills Oct 09 '13 at 21:25
  • I'm not sure it's intentionally case sensitive. It might be that Java is requesting a file from the OS, the OS (Windows in your case) returns a case-insensitive match, and then Java does a case-sensitive string comparison on the returned class file, notes the mismatch and throws NoClassDefFoundError. See http://stackoverflow.com/questions/8105087/capitalization-and-noclassdeffounderror-vs-classnotfoundexception for more info. – lreeder Oct 12 '13 at 14:57