skip to Main Content

So far I have downloaded Apache Commons library , extracted the library
commons-lang3-3.8.1.jar in Javajdk1.8.0_172jrelibext.

Now I have created a class with two fields and I want to compare two objects using
ob1.equals(ob2). Method equals and hashCode have been overridden and the error I’m getting is Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/builder/EqualsBuilder at runtime.

import java.util.*;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;

    class key{

        int end;
        LinkedList<Integer> via = new LinkedList<>();
        key(int x,LinkedList<Integer> ob){
            this.end = x;
            this.via = ob;
        }

        @Override
        public int hashCode(){

            return new HashCodeBuilder().append(end).append(via).toHashCode();

        }

        @Override
        public boolean equals(Object obj)
        {
            if(!(obj instanceof key))
                return  false;
            if(this==obj)
                return true;
            key o=(key)obj;
            return new EqualsBuilder().append(end,o.end).append(via,o.via).isEquals();
        }


    }

    class main{

        public static void main(String[] args)
        {

            key ob1 = new key(12,new LinkedList<Integer>(Arrays.asList(1,2,3)));
            key ob2 = new key(12,new LinkedList<Integer>(Arrays.asList(1,2,3)));

            System.out.println(ob1.equals(ob2));  //expecting true
        }



    }

The details of the error are given below.

Exception in thread "main" java.lang.NoClassDefFoundError: 

org/apache/commons/lang3/builder/EqualsBuilder
        at key.equals(test.java:29)
        at main.main(test.java:43)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.builder.EqualsBuilder
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more

I have been facing this issue for a long time. I have checked all the class files and I’m quite sure that the libraries are loaded properly but I don’t know why I’m getting NoClassDefFoundError at the runtime.

3

Answers


  1. Chosen as BEST ANSWER

    After spending hours on this issue I finally fixed it by setting the CLASSPATH variable. I tried using -cp command but unfortunately that didn't work for me. If we do this explicitly, then you don't need to supply a "-cp" or "-classpath" switch value to the java compiler and java interpreter, since they will already know the updated classpath.

    On my windows machine, I have set the CLASSPATH variable via the following:

    set CLASSPATH=/coding @Octoberlibcommons-lang3-3.8.1.jar;.
    

    Currently, I'm in coding @October directory. The commons-lang3-3.8.1.jar file is located in the       coding @Octoberlib directory.The myapp.java file is located in the coding @October directory.

    enter image description here

    After setting the classpath, I can then compile and execute myapp.java via javac myapp.java command directly and then java myapp to execute the script.


  2. You placed the jar in the correct jrelibext relative path… but it will work only if the java command you run comes from the jrebin directory of the same jre path where you did the change.

    If you copied the correct jar in the extension directory but you get this exception it very probably means that as you run your program you don’t use the JRE where you did the change but another one.
    The java command from the PATH env variable very probably doesn’t refer to the JRE you extended. You can display PATH in your shell to check that.
    So either set the PATH with the java home path of the JRE you extended or just run the java command by specifying the absolute path such as /foo/jre/bin/java main.
    It should (to not say has to) work.

    Login or Signup to reply.
  3. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
        at io.appium.java_client.internal.ElementMap.getElementClass
    

    Answer: add selenium jar "commons-lang3-3.8.1" for resolving this issue

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search