Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Question about RunTime error: NoClassDefFoundError, and how code compiles.

Hi everyone! So I am working on a little Text based adventure game in Java to reinforce all the concepts I've been learning. Part of this is a parser system that takes in a line of text and extracts information. Right now I am working on the movement handling portion, where the direction (NSEW) is passed to after parsing, and then attempts to set the current position by adjusting the [x][y] of a multidimensional array of rooms. This is the background context of the question.

So here is the question... This movement handler object is only accessed through a series of IF statement checks in the parser object. When I compile everything is fine. However when I tested out the program I got a run time error when trying to move east, stating "NoClassDefFoundError". I messed around for awhile with this and eventually linked it back to the .java file never being compiled into a class!

Is this because the call to the movement handler object is within a conditional statement? In my learnings we were always able to compile the main java file and all others would compile with it.

For learning purposes I am using gedit (on ubuntu) and compiling through terminal with javac (JDK).

Does this mean that I have done some sort of bad practice if I have to compile the movement handler separately? That doesn't seem good... Any help is appreciated!

EDIT: Changed class method from static to non-static and instantiated the object in the Main file.. that forced it to be included in the compiling. Still would like to know more about this error though if anyone knows!

2 Answers

This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.

Its quite similar like a return statement. You can't return something in an if statement if there is not a default return or an return written in the else statement. The same goes with your .class file.

Hello thank you so much for the response, the return statement example made it click for me. I realized there was no else to the if that also called the objects method. It was a series of if's rather than if-else with an else, oops.

I did have one question though about the "look for differences in your build time and runtime classpaths". For this do you mean to check each .java file to see there is a .class equivalent? I am only using basic compiling in terminal so the compile path is the same as the source path. In the future I will be using an IDE or javac commands to have a class bin seperate from the java source files. Would I just compare these 2 directories?

Thanks for the help once again, and happy coding :)

Hello michaelcodes,

By "look for differences in your build time and runtime classpaths" I mean that the file was compiled because something depended on it (like you said in tour if statement) but on runtime the class does not exist because it has never run. This creates confusion in the compiler.