Strange "java.lang.NoClassDefFoundError" in Eclipse

Viewed 149010

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?

22 Answers

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?

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.

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

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.

For me (with c++ in eclipse) this happened because the .h was in a different window to the .cpp file.

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

Related