2

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.

Gaskoin
  • 2,469
  • 13
  • 22
itay amit
  • 33
  • 4
  • Can you show your project structure ? Also can you show the other question that ask the same things ? When you are developing, do you see compilation error ? – Elikill58 Oct 20 '21 at 12:08
  • @Elikill58 Yeah sure. Question link: https://stackoverflow.com/questions/42160647/netbeans-class-file-keeps-getting-deleted-when-main-method-is-run Structure link: https://drive.google.com/drive/folders/17idYuzHExbFXah-SW6soSK2QBCPaWLL_?usp=sharing Actually I do see an error when compiling, it's "warning: [options] bootstrap class path not set in conjunction with -source 8 Note: E:\Coding\Github\Tetris\src\Gui\Menu.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 1 warning" – itay amit Oct 20 '21 at 12:33
  • You can [edit] your question to include the image :) but thanks ! – Elikill58 Oct 20 '21 at 12:35
  • @Elikill58 Oh good to know, thanks! – itay amit Oct 20 '21 at 12:37
  • :) What are your JRE/JDK version ? You can check it in your IDE or with path – Elikill58 Oct 20 '21 at 12:38
  • Is there something I can enter in the command prompt to show them? – itay amit Oct 20 '21 at 12:40
  • Open a windows CMD, and enter `echo "%JAVA_HOME% / %JRE_HOME%"` – Elikill58 Oct 20 '21 at 12:41
  • The JDK version is jdk1.8.0_131 but jre_home doesn't give off anything – itay amit Oct 20 '21 at 12:42
  • Ok, that's "normal" it appear not for everyone. And in your IDE, if you right clic on your project and you check for build path ? – Elikill58 Oct 20 '21 at 12:45
  • I don't see an option to see build path once I right click – itay amit Oct 20 '21 at 12:48
  • You are with Intellij Idea ? The software that you are using to dev – Elikill58 Oct 20 '21 at 12:51
  • I am using Apache Netbeans atm – itay amit Oct 20 '21 at 12:51
  • Ok, right-clic on the project, then "Properties" at the end, then "Libraries" then can you check the "Java platform" ? – Elikill58 Oct 20 '21 at 12:53
  • Yeah it says "JDK 16 (Default)" – itay amit Oct 20 '21 at 12:53
  • I made an answer, I got the same type of issue before. It's just your JDK/JRE which are different :) Also, I suggested an edit to add everything in your question. You can accept it if you want, or just let high-rep people check review it ! – Elikill58 Oct 20 '21 at 13:02
  • This doesn't let me to "move this discussion to chat" because of low rep I think – itay amit Oct 20 '21 at 13:12
  • 2
    Does this answer your question? [Why am I getting a NoClassDefFoundError in Java?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java) – egemenakturk Oct 20 '21 at 13:19
  • @egemenakturk No but thanks for the help, it has something to do with jdk/jre incompatibility – itay amit Oct 20 '21 at 13:22
  • 1
    Seems the good old “classes disappear in Netbeans” issue is still there. The last time I stumbled over it [was in 2013](https://stackoverflow.com/a/18406688/2711488) but note that my old answer referred to a “four year old bug” back then, which we cannot look up anymore, because Netbeans has moved to Apache Foundation in the meanwhile and got a new bug-tracking list. However, [this bug report](https://issues.apache.org/jira/browse/NETBEANS-1911) indicates there is still an issue with disappearing classes when “Compile On Save” is active. – Holger Oct 20 '21 at 14:58

3 Answers3

1

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.

egemenakturk
  • 365
  • 1
  • 17
1

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.

Elikill58
  • 4,050
  • 24
  • 23
  • 45
  • I updated to jdk 17.0.1 so I guess so I guess the jre version updated as well, but when using the command: -- echo "%JAVA_HOME% / %JRE_HOME%" -- this shows the same jdk1.8.0_131 in a folder that doesn't even exist on my pc – itay amit Oct 20 '21 at 13:07
  • Oh, so I suggest you to edit the environment variable of your PC, change "JAVA_HOME" to where you install jdk 17. Also don't forget to change in netbeans too – Elikill58 Oct 20 '21 at 13:14
  • I tried doing both of this things, I change the JAVA_HOME to the new directory, and also in the netbeans conf file I changed the netbeans_jdkhome value to the same directory, still it says "bootstrap class path not set in conjunction with -source 8" when compiling – itay amit Oct 20 '21 at 13:20
  • I think something fixed it beacause I have no problem now, thank you so much @Elikill58 – itay amit Oct 20 '21 at 13:32
  • Ok no problem :) Maybe it's because you restart netbeans or something like that ! Happy to helped you :) – Elikill58 Oct 20 '21 at 13:37
0

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.

itay amit
  • 33
  • 4
  • A compilation warning doesn't change something to a *NoClassDefFoundError*. A deprecated warn just say it's not recommended to use it because it will be removed in further update. And such as you compiled with an old jdk, when you run with a newer it's removed. – Elikill58 Oct 21 '21 at 11:54