How to avoid Noclassdeffounderror?

Asked

Viewed 409 times

3

I am developing a project that has many classes, and many files. java, and from time to time, from time to time, this problem happens, randomly. Let’s say: A class that I already used, is working perfectly, suddenly "rebells" and decides to make this mistake.

There is a way to "predict" and "avoid" this error, seeing that it is randomly appearing in my project?

I know how to solve the problem via code (so much so that there are several tutorials about it), but this project will be used in a company, and I will not have time to come and fix this every time the system resolve to "rebel".

  • 1

    How do you solve this problem via code? Just to see if I understand what happens.

  • 2

    Noclassdeffounderror usually happens when Classloader cannot read an informed class. Just pay some attention to the done Imports, and the application dependency libraries are correctly declared in the classpath, if the classes are all correctly in the application, this error will hardly occur.

  • Math, I solve it in a very "rough" way, it’s not really in code, I copy all the contents of the file that I want to "fix" (Ctrl+a and Ctrl+c), delete the file, create a new one with the same name and paste everything back there, it goes back to working normally. diegofm, from what I’ve researched, is exactly what you said, but here’s the thing, I run the code 1 time to do.

  • How are you running the program? Is it from an IDE, console? The problem class is inside a JAR?

  • I’m using the netbeans IDE, but by programming the components and the like into the nail, the windows are Jdialogs and button calls, they’re all in. java, I am currently using only netbeans "run" for testing, but will transfer to JAR when the program is completed

2 answers

2


The mistake NoClassDefFoundError occurs when a class exists, but the ClassLoader Java cannot load or boot it properly.

This can occur in several situations, for example:

  • If you have a static boot block or static attribute that throws an exception, then the class is not loaded.
  • If class fails to load an imported class for any reason.

Since you are running the code in your IDE and solve recreating the class, the problem may simply be that the existing classes are not being recompiled as you make changes.

First, make sure that Netbeans is set to compile classes when saving the file.

If not, use the option Clean and Build Project in all projects involved to force the recompilation of the project.

If not yet resolved, check your dependencies. If there are different versions of libraries on classpath, this means that you may be either loading one version or another version and having random errors.

  • 1

    Thanks for the help, I’ll tidy up here and see if you keep making the mistake, anything put here!

2

According to official Oracle documentation on this error :

Launched if the Java Virtual Machine or a Classloader instance tries to click on the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no class definition could be found. The required class definition existed when the currently running class was compiled, but the definition can no longer be found.

And if you want to take a full look at the documentation you can see here.

That is, this is caused when there is a class file that your code depends on and is present at compile time, but was not found during execution. Look for differences in your build time and run time of classpaths.

Why not use try-catch and put your code for whenever this error occurs,tidy or at least show an error message ?

try
{
// Código que irá aparacer caso não ocorra nenhum erro.
}
catch (NoClassDefFoundError e)
{
// Procure olhar no seu código se está instanciando de forma correta.
}

I advise you to look at the following links to know how to resolve, why they occur and how to prevent this error :

http://javaeesupportpatterns.blogspot.com.br/2012/06/javalangnoclassdeffounderror-how-to.html

http://javaeesupportpatterns.blogspot.com.br/2012/06/javalangnoclassdeffounderror-how-to_15.html

http://javaeesupportpatterns.blogspot.com.br/2012/07/javalangnoclassdeffounderror-how-to.html

http://javaeesupportpatterns.blogspot.com.br/2012/08/javalangnoclassdeffounderror-parent.html

  • Thanks for the help, I will use your tip and the utluiz, anything put here! thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.