[java] Strange "java.lang.NoClassDefFoundError" in Eclipse

I have a Java project in Eclipse perfectly running smoothly until this afternoon, when I updated some files (including a ant build.xml file). When I build the project, the following error appears:

java.lang.NoClassDefFoundError: proj/absa/FrontEnd/ApplicationStarter
Caused by: java.lang.ClassNotFoundException: proj.absa.FrontEnd.ApplicationStarter
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source) at
    java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)  
Exception in thread "main" 

Does anyone have a clue where the problem may be?

This question is related to java eclipse

The answer is


If you are using Eclipse try Project>clean and then try to restart the server


Another occasion of when java.lang.NoClassDefFoundError may happen is during the second/subsequent attempts to instantiate the class after the first attempt failed for some other exception or error. The class loader seems to cache knowledge that it was not able to instantiate the class on the first attempt but not the reason why. Thus it reports NoClassDefFoundError.

So it's a good idea to make sure there were not errors/exceptions with an earlier attempt at instantiating the class before going down a rabbit hole trying to troubleshoot the NoClassDefFoundError.


I had a similar problem and it had to do with the libraries referenced by the java build path; I was referencing libraries that didn't exist anymore when I did an update. When I removed these references, by project started running again.

The libraries are listed under the Java Build Path in the project properties window.

Hope this helps.


For me the issue was I had configured another folder under "Run configurations" which had a class without the latest changes. Once I got it removed referred to the correct bin folder, it started working. Hope it helps someone.


This seems to be a common error. The solution is to:

  • right-click project
  • choose properties
  • choose 'Java Compiler'
  • unclick the first box saying 'Enable project specific settings'
  • apply
  • save
  • run

Hope this helps in some cases.


As mentioned above, "java.lang.ClassNotFoundException means CLASSPATH issues."

In my setup, I am running Maven to build (instead of Ant) and using Eclipse (instead of Netbeans).

Usually, to build and setup the project, I will run 'mvn clean', 'mvn compile', 'mvn eclipse:eclipse' from the Windows command prompt. The last command 'mvn eclipse:eclipse' updates the project configuration creating .classpath and .project files.

To fix the problem, I deleted the two files (.classpath and .project) then re-ran the three commands.

So depending on your configuration, try to find the classpath/project files (make a backup) and delete them. You can also try deleting the target/release/build folder (whatever is created from the build command) as well. Then try to build/package/configure your project again.


go to build path and check for errors in the jar files, they might be moved to somewhere else. if you have errors on the jar files. Remove them and locate them by clicking add external jars.

-cnufederer


This worked for me in eclipse:

  1. Goto Project -> Properties
  2. Click on Source tab
  3. Clear the checkbox -> Allow output folders for source folders
  4. Apply the changes and run the project

both the problem will be because of classpathref="master-classpath", please check the value is correct


I too was facing the same issue. Then discovered that the path for the lib folder in the classpath was not set properly.


Doing Project->Clean… in Eclipse solved this problem for me.


I thought my problem and its solution could help.So i was getting this same error in my eclipse project.In my project i have couple of jar files and the NOCLASSDEFERROR was thrown for a file in the jar file.

My library files were part of a folder name "lib" in my project heirarchy.I changed my folders name to "libs" and voila it worked.

(I looked into the .classpath file and i had key-value pairs,and the entry for my jar file had key named "lib" and hence i thought probably changing from lib could help.)


While this is a wild guess and may not be applicable in your specific situation, this could've saved me an hour or so.

In case you have "converted" a plain project into Java project (by editing .project file and adding appropriate tag), make sure you also have a proper specified - my project didn't get built even though Eclipse attempted to and run no builders (success!):

<buildSpec>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>

I found that I had a jar in WEB-INF/lib that was in my classpath, however when I upgraded it to the latest version, the filename was different. Removing the old jar file from the classpath and adding the new one fixed the problem. Strangely, Eclipse did not seem to warn me that the old jar file was missing and appeared to compile, however nothing was getting compiled, hence the NoClassDefFoundError. The clue for me was that the build/classes directory was setup in the project as the output folder but no class files were getting created there after the build.


Ensure that all the libraries you're using in your project are not being referenced from other project int the workspace. That was my problem.


I'm seeing this a bit too often lately. Just today I had the issue with a class in the same package as the affected (red-flagged) class !

Exiting eclipse and restarting generally works to resolve the red flag on the affected class but sometimes a red flag is left on the project, then I also need to close the project and reopen it as well to get rid of the standalone red flag. It looks quite weird to see a red flag on a project, with no red flags in any of its child directories.

With maven project clusters, I close and open all of the projects in the cluster after restarting eclipse.


java.lang.ClassNotFoundException means CLASSPATH issues. Not having a clue implies that you're assuming that the CLASSPATH is set properly, but it's not.

If you're building with Eclipse, make sure that the directory where your compiled .class files exists, is in the CLASSPATH, and has the necessary .class files in it.

If you're building with Ant, make sure you see something like this in your build.xml:

<path id="production.class.path">
    <pathelement location="${production.classes}"/>
    <pathelement location="${production.resources}"/>
    <fileset dir="${production.lib}">
        <include name="**/*.jar"/>
        <exclude name="**/junit*.jar"/>
        <exclude name="**/*test*.jar"/>
    </fileset>
</path>

UPDATE: Either you don't have JAVA_HOME/bin in your PATH or you downloaded the JRE instead of the JDK. Check the directory where you installed Java and see if your /bin directory has javac.exe in it. If you don't have a /bin, download the JDK and install it.

If you do have a /bin with javac.exe in it, check your PATH to see that the Java /bin directory is in the PATH. Once you've set it, open a command shell and type "javac -version" to ensure that all is well.

What JDK did you tell Eclipse to use?


When you launch your program, it stores the launch configuration that you may latter modify. If something changed in your build/run process, you may have wrong settings. For example I used to work with maven, and some launch configuration reference a maven2_classpath_container. When deleting the launch configuration and running the program again, it can work again.


BY ABDULHAYEE:

NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time. For example if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw NoClassDefFoundError. It’s important to understand that this is different than ClassNotFoundException which comes while trying to load a class at run-time only and name was provided during runtime not on compile time. Many Java developer mingle this two Error and gets confused.

In short NoClassDefFoundError will come if a class was present during compile time but not available in java classpath during runtime. Normally you will see below line in log when you get NoClassDefFoundError: Exception in thread "main" java.lang.NoClassDefFoundError Exception in thread “main” simply indicate that its “main” thread which is not able to find a particular class it could be any thread so just don’t worry . Difference between this error coming in main thread and other thread is , whenException in thread “main” comes program crashes or shut it self down as opposed to other thread in which case your program will continue to run.,

Read more: http://javarevisited.blogspot.com/2011/06/noclassdeffounderror-exception-in.html#ixzz2jr35SsaZ


Stab in the dark, but I was having the same exact issue. I solved it by deleting the project from my workspace (be careful not to delete from disk), and then re-imported the project and it worked fine. I think mine was caused by a bad windows shutdown (restarting windows correctly did not correct the issue). HTH.


I see that people have already talked about class path. Since there is no accepted answer, I assume it is not related to class path. So I would like to add that, not having package directive can also lead to class not found errors.