4

I have Exception in thread "main" java.lang.NoClassDefFoundError: A (wrong name: a) and I dont't have any idea what this can caused by

public class Test
{
    public static void main(String[] args)
    {
        new B();
    }
}

interface a { }

class A implements a { }

class B extends A { }

Edit: in online compiler https://www.onlinegdb.com/online_java_compiler it compiles

8
  • 1
    Is this all one file? Interfaces and classes should be capitalized, anyway Commented Sep 1, 2020 at 22:48
  • yes, named Test.java Commented Sep 1, 2020 at 22:49
  • 1
    And what are you using to run the file? Commented Sep 1, 2020 at 22:49
  • What is your Java version? Commented Sep 1, 2020 at 22:50
  • 2
    What operating system are you using? Is this Windows? Commented Sep 1, 2020 at 22:53

2 Answers 2

11

When Java compiles your source code, it creates multiple .class files. For example, it creates Test.class for public class Test, a.class for interface a, and A.class for class A. The problem here is that file names in some operating systems are case-insensitive. This means that the operating system sees a.class and A.class as the same file so one will overwrite the other.

The online compiler most likely treats these file names as different due to case-sensitivity.

The solution here is to use different names so that you avoid these name collisions at the operating system level.

The established Java convention is to start all class and interface names with an upper case letter. If you follow this convention, then you will avoid this problem.

Sign up to request clarification or add additional context in comments.

7 Comments

In addition, type names in Java should always be Capitalized.
"Test$a.class for interface a" why? a is not nested type.
@Pshemo I was thinking that the Java compiler would generate class files with the $ syntax for private classes. I'm probably mistaken in that detail. The underlying problem is still how the OS deals with case differences in file names.
"So for private classes Java won't create a file with the $ syntax" well, private classes can only exist within other classes (are nested) so they will be in form OuterClass$InnerClass but here classes are not private, they don't have have specified access modifiers so they are package-private - they belong to entire package (its access is allowed only to elements from same package).
@Pshemo So what will be the names of the generated class files?
|
1

If you run javac path/to/your/file, you should see the list of .classfiles created by the java compiler in that dir. The problem with your approach is you have duplicate names for the interface and the class i.e A (case insensitive) and as a result only one .class gets created. Try again by changing the name of either interface or class and your problem should go away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.