0

I am using spring boot to start a RMI server application using java8. In very rare cases:

  • the application fails to initialize classes, throwing "java.lang.NoClassDefFoundError: Could not initialize class X".
  • X is not always the same Class, but so far, all these classes have initialization of static members in common. (not always directly, so the stacktrace does not always point to a static variable...)
  • The java process still has a valid read filehandle to the jar.
  • The jar file didn't change after start of the process.
  • memory is not an issue.

As the problem is very, very rare, i am not able to reproduce it.

How would you continue to find the problem?

Is it possible to get more information about the real cause of the NoClassDefFoundError?

Henning
  • 1,289
  • 2
  • 11
  • 25
  • No ClassDefFoundError can usually be solved by setting the classpath when executing your jar. Java is probably not looking in the right place and you need to give it some help. – Adam Jun 19 '18 at 15:29
  • Just get rid of all static initializers, especially those that interact with outside services or the file system. – Sean Patrick Floyd Jun 19 '18 at 16:32
  • Does this answer your question? [Possible reasons for NoClassDefFoundError](https://stackoverflow.com/questions/12606477/possible-reasons-for-noclassdeffounderror) – Gunnar Jul 20 '22 at 06:01

2 Answers2

0

java.lang.NoClassDefFoundError:

means, that the class which you are trying to run was not found in the classpath.

You need to add the class or .jar file which contains this class into the java classpath.

The error occurs when a compiler could successfully compile the class, but Java runtime could not locate the class file. It usually happens when there is an exception while executing a static block or initializing static fields of the class, so class initialization fails.

Fix: 1.make sure whether class or jar containing that class is available in the classpath.

2.classpath then most probably classpath is getting overridden

3.if an application is using multiple class loaders, classes loaded by one classloader may not be available by other class loaders.

Varun
  • 196
  • 1
  • 20
-1

NoClassDefFoundError occurs when the class was present during Compile time and so the program successfully compiled but not available during Runtime for any reason.

In J2EE environment it might be a case when one get NoClassDefFoundError even if the class is present because it may not be visible to the corresponding class loader.

It is really hard to diagnose and fix this problem as the problem is unavailability of the class file at runtime in the classpath. But you can try these to avoid this problem.

  1. Make sure if the class or jar containing that class is available in the classpath.
  2. If it’s available on application’s classpath then most probably classpath is getting overridden. To fix that you need to find the exact classpath used by your application.
  3. If an application is using multiple class loaders then classes loaded by one classloader may not be available by other class loaders.
Sundeep
  • 452
  • 2
  • 12