NoClassDefFound Error: Could not initialize class

Suppose that you are faced with an exception like this:

java.lang.NoClassDefFoundError: Could not initialize class com.example.YourClass

At first, you might think that java cannot find com.example.YourClass. But if you execute this line:

System.out.println(com.example.YourClass.class.getName());

Then it will work successfully and you will not see an exception.

The point is that java sees the class, but a masked error occurred while initializing static fields in this class.

It is necessary to find out during the initialization of which particular field the error occurs and fix it. After that, the exception “Could not initialize class” will disappear.

Leave a Reply

Your email address will not be published.