Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.builder.EqualsBuilder

So far I have downloaded Apache Commons library , extracted the library
commons-lang3-3.8.1.jar in Java\jdk1.8.0_172\jre\lib\ext.

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.

like image 224
suvojit_007 Avatar asked Oct 28 '25 09:10

suvojit_007


1 Answers

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 @October\lib\commons-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 @October\lib 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.

like image 160
suvojit_007 Avatar answered Oct 31 '25 07:10

suvojit_007



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!