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
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:Currently, I'm in
coding @October
directory. Thecommons-lang3-3.8.1.jar
file is located in thecoding @Octoberlib
directory.Themyapp.java
file is located in thecoding @October
directory.After setting the classpath, I can then compile and execute
myapp.java
viajavac myapp.java
command directly and thenjava myapp
to execute the script.You placed the jar in the correct
jrelibext
relative path… but it will work only if thejava
command you run comes from thejrebin
directory of the samejre
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 thePATH
env variable very probably doesn’t refer to the JRE you extended. You can displayPATH
in your shell to check that.So either set the
PATH
with the java home path of the JRE you extended or just run thejava
command by specifying the absolute path such as/foo/jre/bin/java main
.It should (to not say has to) work.
Answer: add selenium jar "commons-lang3-3.8.1" for resolving this issue