3

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.

suvojit_007
  • 1,690
  • 2
  • 17
  • 23
  • 1
    You should try putting the commons-lang3-3.8.1.jar on a classpath. For more details see [this](https://stackoverflow.com/questions/8084926/including-jar-files-in-class-path). – Ivan Kulezic Oct 05 '18 at 15:42
  • @IvanKulezic I have tried putting the commons-lang3-3.8.1.jar on a classpath but it's giving the same error. – suvojit_007 Oct 05 '18 at 15:51
  • 2
    I managed to make it work by compiling with this command: `javac -cp commons-lang3-3.8.1/commons-lang3-3.8.1.jar:. key.java` And then run it using this command: `java -cp commons-lang3-3.8.1/commons-lang3-3.8.1.jar:. main`. As output I got true. – Ivan Kulezic Oct 05 '18 at 16:00

3 Answers3

1

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.

suvojit_007
  • 1,690
  • 2
  • 17
  • 23
  • I got this error because I had `-cp` for `javac` but not for `java`: `java -cp commons-lang3-3.9.jar:. MyApp` – John Oct 14 '19 at 16:31
0

You placed the jar in the correct jre\lib\ext relative path... but it will work only if the java command you run comes from the jre\bin 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.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
0
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

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 20 '21 at 17:39