Java .class file disappeares as soon as I run the project

Viewed 203

I've been on a project where I build tetris that has multiplayer, and it's been working for a couple of days with no problem, but now suddenly there's this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: Game/Piece

This basically means it can't find the .class of my class called Piece, but whenever I do a "clean and build" the .class file appears, then instantly disappears when I run the project.

This is my project structure (running with JDK 16, in netbeans):

enter image description here

Is there a reason for this exception?

My JAVA_HOME variable: jdk1.8.0_131.

3 Answers

While it's possible that this is due to a classpath mismatch between compile-time and run-time, it's not necessarily true.

It is important to keep two or three different exceptions straight in our head in this case:

java.lang.ClassNotFoundException This exception indicates that the class was not found on the classpath. This indicates that we were trying to load the class definition, and the class did not exist on the classpath.

java.lang.NoClassDefFoundError This exception indicates that the JVM looked in its internal class definition data structure for the definition of a class and did not find it. This is different than saying that it could not be loaded from the classpath. Usually this indicates that we previously attempted to load a class from the classpath, but it failed for some reason - now we're trying to use the class again (and thus need to load it, since it failed last time), but we're not even going to try to load it, because we failed loading it earlier (and reasonably suspect that we would fail again). The earlier failure could be a ClassNotFoundException or an ExceptionInInitializerError (indicating a failure in the static initialization block) or any number of other problems. The point is, a NoClassDefFoundError is not necessarily a classpath problem.

And this question has been asked before.

This type of error can appear for multiple reasons :

  1. Because the compiler is too different than the JVM, specially with new Java syntax.

Personally, I had this type of error by running Java 16 code with Java 8 JRE, and I fix it by removing the package with the Java 16 code.

  1. Because you were using deprecated class, which have been removed since this date.

So, to fix your issue, there is multiple ways:

  • Use same Java in Netbeans and in your project config (to prevent change)
  • Upgrade your Java JRE installed (instead of old Java 8 version) to have both Java 16
  • Downgrade Netbeans JDK to Java 8 to have both Java 8

The objective is to use the same JVM than JDK, to see all warns/error that are linked to your current code.

So apperantly I had a warning (not an error) when compiling, it said something was depracated, so I had to add a compiler option name -Xlint:(something here I can't remember), so when I compiled it the issue was obvious, JFrame.hide() is a depracated function so I just had to remove it. It's weird that the runtime error was "NoClassDefFoundError" which has nothing to do with the issue.

Related