Tuesday, 1 June 2010

Java Wrong Name Error when Running a class

Duke Waving

Problem: You have compiled a class from a source file in a package. For example:

javac mypackage\MyClass.java



The class compiles fine. But when you go to run it you get an error.

java mypackage\Myclass



results in:

Exception in thread "main" java.lang.NoClassDefFoundError: Myclass(wrong name: mypackage/MyClass)



Solution: You can not use a backslash in a Java class path. Instead you need to use a forward slash.

java mypackage/Myclass



Better yet, as a best practice, always use a dot to specify the class name. This avoids the slash issue altogether.

java mypackage.Myclass



That should solve any future runtime errors for classes in a package.

No comments:

Post a Comment