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

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.

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.
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.

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.

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

Related

Unsure about reason behind NoClassDefFoundError

I have built a DLL which I am attempting to wrap Java code with, however I am having some troubles with running my Java program. I wrote a simple test DLL and Java program and am producing the same error, and although there are plenty of resources regarding NoClassDefFoundError online I can't seem to solve mine with any troubleshooting methods.
Here is my D:\Test1.Java file
public class Test1 {
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
public native void displayHeyLand();
public static void main (String[] args) {
Test1 t = new Test1();
t.displayHeyLand();
}
}
After compiling, attempting to run D:\Test1.classresults in the following:
D:\>java Test1.class
Exception in thread "main" java.lang.NoClassDefFoundError: Test1.class
Caused by: java.lang.ClassNotFoundException: Test1.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
Could not find the main class: Test1.class. Program will exit.
Why I am stumped :
1. I have set my classpath to be D:\, so I believe my class definition would be in the classpath, and I do not see how my compile-time and run-time classpaths could be any different.
2. I don't see how this could have anything to do with static initialization, and I believe the exception would look different.
Perhaps I'm just missing something incredibly simple, I am very newbie with Java.
Any help is greatly appreciated!
The classpath environmental variable is taking precedence over that in the java run command. You need to specify the class location (as well as removing the .class file extension)
java -cp . Test1
Java normal syntax for executing class file is
Java [<options>....} <class-name> [<arguments>....]
For example
java com.package.name.Test1
here how compiler works
1. Compiler search for complete class name
2. Load that class
3. check for main method - in the same class
4. Call main method with passed arguments in command line string.
Now following are the possibilities why your class may not found main method.
1 - forgot to include package name
I am new developer in java but I found when I run application using eclips or intellJ editor it gives different path and package name and execute code as I noticed it on command line edior. So make sure you are including package name
For example:
java com.package.name.Test1 instead of
java Test1
2. File name or pathname rather then class name
As I noticed output file is in different location. That why class file path was different.
java Test1.class
java com/package/name/Test1.class
3. Typo
also I noticed you are using
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
Is this function ? or constructor? If it is function then where is name of the function? You cant write code without any reference in classs

ClassNotFoundException when running in Command Prompt

I am very new to java and tried to run a simple code of calculating volume. The code is below:
package chapter6;
class Box {
double width;
double height;
double depth;
}
package chapter6;
public class BoxDemo {
public static void main(String[] args) {
Box myBox = new Box();
double vol;
myBox.depth = 1;
myBox.height = 2;
myBox.width = 3;
vol = myBox.depth * myBox.height * myBox.width ;
System.out.println("Volume: " + vol);
}
}
I am able to run the code from eclipse, but when i try to run the code in Command Prompt i get the error:
C:\Prabhjot\Java\CompleteRefence\build\classes>java BoxDemo.class
Exception in thread "main" java.lang.NoClassDefFoundError: BoxDemo/class
Caused by: java.lang.ClassNotFoundException: BoxDemo.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: BoxDemo.class. Program will exit.
Please assist.
First class file should be at this location:
C:\Prabhjot\Java\CompleteRefence\build\classes\chapter6\BoxDemo.class
Then you should be inside:
C:\Prabhjot\Java\CompleteRefence\build\classes>
Then issue the command:
java chapter6.BoxDemo
You have put your class in a package called chapter6. This means that the java file should be in a folder called chapter6 in the class root folder of your project. And when you run it, you should be in the root folder and use the command java chapter6.BoxDemo
There is mistake in how you are running your program from console.
You are doing
java BoxDemo.class
But you need to do only
java BoxDemo
While running your program you don't need to mention .class with the name.
and if you are accessing it from root folder then you need to do
java chapter6.BoxDemo
try this
C:\Prabhjot\Java\CompleteRefence\build\classes>java chapter6.BoxDemo (RUN)
There is no need to specify .class extinction to the file while running.After compiling the java file it will create the .class file.
EXAMPLE
When you invoke BoxDemo.class, Java looks for a class called class in the BoxDemo package, which doesn't exist. As you can see from the output java.lang.NoClassDefFoundError: BoxDemo/class, it's searching for a directory BoxDemo.
Instead, just specify the class name: BoxDemo; e.g. java BoxDemo.

javacpp in eclipse doesn't work --ClassNotFoundException (using JNI)

I'm trying to use javacpp and encounter some difficulties with eclipse (+ mac OS).
When I run this in my command line - it works fine:
#include <string>
namespace LegacyLibrary {
class LegacyClass {
public:
const std::string& getProperty() { return property; }
void setProperty(const std::string& property) { this->property = property; }
private:
std::string property;
};
}
and
import com.googlecode.javacpp.*;
import com.googlecode.javacpp.annotation.*;
#Platform(include="LegacyLibrary.h")
#Namespace("LegacyLibrary")
public class LegacyLibrary {
public static class LegacyClass extends Pointer {
static { Loader.load(); }
public LegacyClass() { allocate(); }
private native void allocate();
public native #ByRef String getProperty();
public native void setProperty(String property);
}
public static void main(String[] args) {
LegacyClass l = new LegacyClass();
l.setProperty("Hello World!");
System.out.println(l.getProperty());
}
}
as suggested in this post
I get the end result: "hello world".
Now I'm taking this into my eclipse IDE and for some reason, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: java.lang.ClassNotFoundException: com.test.LegacyLibrary
at com.googlecode.javacpp.Loader.load(Loader.java:293)
at com.googlecode.javacpp.Loader.load(Loader.java:271)
at com.test.LegacyLibrary$LegacyClass.<clinit>(LegacyLibrary.java:12)
at com.test.LegacyLibrary.main(LegacyLibrary.java:23)
Caused by: java.lang.ClassNotFoundException: com.test.LegacyLibrary
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.googlecode.javacpp.Loader.load(Loader.java:291)
... 3 more
I placed the 2 files into a package com.test and the javacpp.jar I placed in the classpath.
Is there anything I'm missing here?!?! the error message indicates that something in the classpath is incorrect (or missing), but what is it? (I also tried to point in the properties to the javacpp.jar in a different folder but I get the same error message). Maybe VM arguments?!?!
Thank you!
Did you add the javacpp.jar file to the projects libraries? Go to the project properties, then the Build Path information, there should be a tab for Libraries. Add an External Jar file, and select the javacpp.jar file.
Go to Project Properties->Java Build Path, in the tab Source click on the arrow to expand the source folder, then select Native Library Location and click on Edit, After all find the path to the LegacyLibrary.h file and that should work.
My guess is that your maven file is misconfigured and hence gcc compiler cannot find your source file LegacyLibrary.h.
Configure your pom.xml using the following link as a reference.
https://github.com/oltzen/JavaCppExample/blob/master/pom.xml
Pay attention to line 53 and 65. Fill in the correct package names. This helps the compiler find out where your LegacyLibrary.h is.
Also, watch this video clip https://www.youtube.com/watch?v=LZrrqZLhtmw which walks you through the whole process how to run Javacpp with maven and eclipse.

Matlab + Java: java.lang.ExceptionInInitializerError when trying to use javabuilder

I've been trying to use Matlab's javabuilder package under Windows XP, but I'm getting a strange error when trying to instantiate any javabuilder class. To illustrate the problem, I've created a simple program that prints the MCRROOT and PATH system variables (to check if they're correctly set) and tries to create a MWCharArray:
import com.mathworks.toolbox.javabuilder.*;
import com.mathworks.toolbox.javabuilder.internal.MCRConfiguration;
class Main
{
public static void main(String[] args)
{
System.out.println("MCRROOT: " + System.getenv("MCRROOT"));
System.out.println("PATH: " + System.getenv("PATH"));
System.out.println(MCRConfiguration.isInstalledMCR());
MWCharArray test = new MWCharArray("Test");
}
}
When I execute the program, the output is:
MCRROOT: C:\Program files\MATLAB\MATLAB Compiler Runtime\v710
PATH: C:\Program files\CollabNet Subversion Client;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program files\MATLAB\MATLAB Compiler Runtime\v710
false
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getMCRRoot(MCRConfiguration.java:77)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$ModuleDir.<clinit>(MCRConfiguration.java:51)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getModuleDir(MCRConfiguration.java:56)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.<clinit>(MWMCR.java:1447)
at com.mathworks.toolbox.javabuilder.MWUtil.GetUnknownClassID(MWUtil.java:1258)
at com.mathworks.toolbox.javabuilder.MWClassID.<clinit>(MWClassID.java:41)
at com.mathworks.toolbox.javabuilder.MWCharArray.<init>(MWCharArray.java:75)
at Main.main(Main.java:11)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1937)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$MCRRoot.get(MCRConfiguration.java:70)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$MCRRoot.<clinit>(MCRConfiguration.java:72)
... 8 more
Java Result: 1
First of all, are MCRROOT's and PATH's values correct? I've tried google for finding out how to set MCRROOT, but there are conflicting results: some sources say that I should include de version dir, others say the opposite. Also, why is the isInstalledMCR method returning false? I've double-checked the MCR installation (and even uninstalled and installed it to be sure), so why isn't the library finding it?
Thanks on advance for any help!
Edit: I've also tried setting MCRROOT with no version string, and it also fails.
Just wild guessing! Java is messing around with strings, while your 'mcrroot' contains white spaces. I might change the mcr install path to something like C:\MATLAB\MATLABCompilerRuntime\v710, omitting any white spaces and special characters.
I've found the solution, so I'm post a self answer for future reference: Besides adding the javabuilder.jar to the program's classpath, you also have to add the path to the MCR's runtime libraries to the java.library.path JDK parameter.
My mistake was that, instead of setting the path as the path to the libraries at the MCR installation directory (On my case, C:\MATLAB\MCR\v710\runtime\win32), I copied the runtime directory to my project's dir and used it instead. It seems that the javabuilder library uses the java.library.path variable the guess the MCROOT, what would explain the weird "StringIndexOutOfBoundsException".

How to execute a java .class from the command line

I have a compiled java class:
Echo.class
public class Echo {
public static void main (String arg) {
System.out.println(arg);
}
}
I cd to the directory and enter: java Echo "hello"
I get this error:
C:\Documents and Settings\joe\My Documents\projects\Misc\bin>java Echo "hello"
Exception in thread "main" java.lang.NoClassDefFoundError: Echo
Caused by: java.lang.ClassNotFoundException: Echo
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Echo. Program will exit.
What is the simplest way to get my java code in a form that I can run from the command line as apposed to having to use Eclipse IDE?
Try:
java -cp . Echo "hello"
Assuming that you compiled with:
javac Echo.java
Then there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions )
If that's the case and listing the contents of your dir displays:
Echo.java
Echo.class
Then any of this may work:
java -cp . Echo "hello"
or
SET CLASSPATH=%CLASSPATH;.
java Echo "hello"
And later as Fredrik points out you'll get another error message like.
Exception in thread "main" java.lang.NoSuchMethodError: main
When that happens, go and read his answer :)
With Java 11 you won't have to go through this rigmarole anymore!
Instead, you can do this:
> java MyApp.java
You don't have to compile beforehand, as it's all done in one step.
You can get the Java 11 JDK here: JDK 11 GA Release
You need to specify the classpath. This should do it:
java -cp . Echo "hello"
This tells java to use . (the current directory) as its classpath, i.e. the place where it looks for classes. Note than when you use packages, the classpath has to contain the root directory, not the package subdirectories. e.g. if your class is my.package.Echo and the .class file is bin/my/package/Echo.class, the correct classpath directory is bin.
You have no valid main method... The signature should be:
public static void main(String[] args);
Hence, in your case the code should look like this:
public class Echo {
public static void main (String[] arg) {
System.out.println(arg[0]);
}
}
Edit: Please note that Oscar is also right in that you are missing . in your classpath, you would run into the problem I solve after you have dealt with that error.
If you have in your java source
package mypackage;
and your class is hello.java
with
public class hello {
and in that hello.java you have
public static void main(String[] args) {
Then
(after compilation)
changeDir (cd) to the directory where your hello.class is.
Then
java -cp . mypackage.hello
Mind the current directory and the package name before the class name.
It works for my on linux mint and i hope on the other os's also
Thanks Stack overflow for a wealth of info.
My situation was a little complicated. I had to do three steps since I was using a .dll in the resources directory, for JNI code. My files were
S:\Accessibility\tools\src\main\resources\dlls\HelloWorld.dll
S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.class
My code contained the following line
System.load(HelloWorld.class.getResource("/dlls/HelloWorld.dll").getPath());
First, I had to move to the classpath directory
cd /D "S:\Accessibility\tools\src\test\java"
Next, I had to change the classpath to point to the current directory so that my class would be loaded and I had to change the classpath to point to he resources directory so my dll would be loaded.
set classpath=%classpath%;.;..\..\..\src\main\resources;
Then, I had to run java using the classname.
java com.accessibility.HelloWorld
First, have you compiled the class using the command line javac compiler? Second, it seems that your main method has an incorrect signature - it should be taking in an array of String objects, rather than just one:
public static void main(String[] args){
Once you've changed your code to take in an array of String objects, then you need to make sure that you're printing an element of the array, rather than array itself:
System.out.println(args[0])
If you want to print the whole list of command line arguments, you'd need to use a loop, e.g.
for(int i = 0; i < args.length; i++){
System.out.print(args[i]);
}
System.out.println();

Categories

Resources