Home java Why does java.lang.NoClassDefFoundError occur?

Why does java.lang.NoClassDefFoundError occur?

Author

Date

Category

When using this code:

package javaapplication;
import com.github.sardine.DavResource;
import com.github.sardine.Sardine;
import com.github.sardine.SardineFactory;
import java.io.IOException;
import java.util.List;
public class JavaApplication {
  public static void main (String [] args) throws IOException {
    Sardine sardine = SardineFactory.begin ("login", "password");
    List & lt; DavResource & gt; resources;
    resources = sardine.list ("http://yourdavserver.com/adirectory/");
    for (DavResource res: resources)
    {
      System.out.println (res);
    }
  }
}

An error occurs NoClassDefFoundError :

run: Exception in thread “main” java.lang.NoClassDefFoundError:
org / apache / http / HttpEntity at
com.github.sardine.SardineFactory.begin (SardineFactory.java:44) at
com.github.sardine.SardineFactory.begin (SardineFactory.java:34) at
javaapplication.JavaApplication.main (JavaApplication.java:25) Caused
by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity at
java.net.URLClassLoader.findClass (URLClassLoader.java:381) at
java.lang.ClassLoader.loadClass (ClassLoader.java:424) at
sun.misc.Launcher $ AppClassLoader.loadClass (Launcher.java:331) at
java.lang.ClassLoader.loadClass (ClassLoader.java:357) … 3 more
C: \ Users \ Levchenko \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml: 53:
Java returned: 1

Please tell me why the error occurs. Perhaps I have not included all the libraries I need.

Added libraries. There are now 3 libraries in the project structure: sardine, httpclient, http core. Here’s what happened:

Exception in thread “main” java.lang.NoClassDefFoundError:
org / apache / commons / logging / LogFactory at
org.apache.http.conn.ssl.AbstractVerifier. (AbstractVerifier.java:61)
at
org.apache.http.conn.ssl.AllowAllHostnameVerifier. (AllowAllHostnameVerifier.java:44)
at
org.apache.http.conn.ssl.AllowAllHostnameVerifier. (AllowAllHostnameVerifier.java:46)
at
org.apache.http.conn.ssl.SSLConnectionSocketFactory. (SSLConnectionSocketFactory.java:146)
at
com.github.sardine.impl.SardineImpl.createDefaultSecureSocketFactory (SardineImpl.java:1116)
at
com.github.sardine.impl.SardineImpl.createDefaultSchemeRegistry (SardineImpl.java:1099)
at
com.github.sardine.impl.SardineImpl.configure (SardineImpl.java:1064)
at com.github.sardine.impl.SardineImpl. (SardineImpl.java:203)
at com.github.sardine.SardineFactory.begin (SardineFactory.java:44)
at com.github.sardine.SardineFactory.begin (SardineFactory.java:34)
at javaapplication.JavaApplication.main (JavaApplication.java:25)
Caused by: java.lang.ClassNotFoundException:
org.apache.commons.logging.LogFactory at
java.net.URLClassLoader.findClass (URLClassLoader.java:381) at
java.lang.ClassLoader.loadClass (ClassLoader.java:424) at
sun.misc.Launcher $ AppClassLoader.loadClass (Launcher.java:331) at
java.lang.ClassLoader.loadClass (ClassLoader.java:357) … 11 more
C: \ Users \ Levchenko \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml: 53:
Java returned: 1


Answer 1, authority 100%

Error NoClassDefFoundError occurs when a class which your application depends on is not available when executing your code.

Description of the error in the documentation:

Thrown if the Java Virtual Machine or a ClassLoader instance tries to
load in 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
definition of the class could be found.
The searched-for class
definition existed when the currently executing class was compiled,
but the definition can no longer be found.

Free translation:
Thrown when the Java Virtual Machine or class loader tries to load a class definition (either by a standard method call or by instantiating a class from a constructor) and the class definition could not be found.
The sought class definition existed when the current class was compiled, but cannot be found during execution

The error is most likely caused by the fact that one of the libraries required for your application to work is not added to the classpath.

To fix the error you need:

  • look at the missing class name in the error message;
  • check when accessing which class an error occurs;
  • find the library (dependency if using the Maven build system, Gradle) where the class is located;
  • check that the library is available when executing the code (the JAR file of the library is in the classpath).

Similar question on Stackoverflow English:

Your example

In this case, the class name is org / apache / http / HttpEntity. It is located in the Apache HttpComponents Core library. You can download it from the Maven repository (https://mvnrepository.com/artifact /org.apache.httpcomponents/httpcore/4.4.3 ).

Sardine depends on the library Apache HttpComponents Client version 4.5.1 , which in turn depends on Apache HttpComponents Core version 4.4 .3.
You will most likely need to include both of these libraries at least.

Add libraries and try again.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions