java.lang.NoClassDefFoundError web service - java

I developed a web service in java that makes addition, subtraction, division and multiplication. Then, I created a class named CallCalc in another project named CallCalculator. I wrote some code in the class to call the web service. When I run the class CallCalc with parameters in eclipse, it returns to me the correct result, but when I run the class CallCalc with parameters in cmd, it throws an exception like;
Exception in thread "main" java.lang.NoClassDefFoundError: com\example\ws\calculator\CalculatorSEI
at CallCalc.main<CallCalc.java:32>
Caused by: java.lang.ClassNotFoundException: com.example.ws.calculator.CalculatorSEI
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>
... 1 more
I searched for a long time, tried different ways but could not correct it and I'm really confused. Maybe its answer is very simple, forgive me for that.
Does anybody have an idea about how to solve it? (I can post the necessary code block if you need)

Is you project using maven? If so.. Do you have a dependency to the "SEI module" where CalculatorSEI is in?
A NoClassDefFoundError occurs when you're using a class that is unknown at runtime hence you probably don't have a dependency to it.

Related

NoClassDefFoundError while testing azure purview SDK for Java

I'm following this sample to test purview SDK for java, it could be built successful, but when I run this java file ,it shows "Exception in thread "main" java.lang.NoClassDefFoundError: org/reactivestreams/Publisher". the class "DefaultAzureCredentialBuilder" was defined in core-identity jar and I already imported it ,not clear why it still failed and what class it required?
here is my code content
We get this error when the class is not available in the program at runtime but class was successfully compiled by the java compiler.
The reason why you are getting this error is because of jre unable to find DefaultAzureCredentialBuilder class.
As you mentioned in the post that this class is defined inside core-identity jar. whether the jar file may be missing. So, try to import it again and check.

NoClassDefFoundError when running Jar

I have this Problem. When i run my code in Intellij it works fine, but if i do an artifact and build the jar, it doesnt work. I think its caused by an extern library. Heres my output:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mindfusion/scheduling/Calendar
at GUI.<init>(GUI.java:75)
at Logfiles.main(Logfiles.java:13)
Caused by: java.lang.ClassNotFoundException: com.mindfusion.scheduling.Calendar
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 2 more
I know which Class it is but i dont know how to solve the Problem. Im really just a beginner. Could you please help me and explain it simple. Thank you
Edit:
After i build the artifact with extracted Libraries this Error comes : Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
This error simply means the class file is not present in the jar.
One possible solution is you can download jd-gui which is used to look at jars. You can use this to check if the class is present.
Another solution is you can grep search the class in the jar with this simple command.
grep -l "<class-name>" <jar-name>.jar
if the class is not present in the jar file. you can add the class using jar command.
jar -cvf <jar-absolute-location> <class-path>
eg : jar -cvf GUI.jar com.mindfusion.scheduling.Calendar
The easiest way to understand this issue, it to read the Javadoc for that class. From the Javadoc:
Thrown if the Java Virtual Machine or a ClassLoader instance tries to
load in the definition of a class (as part of a normal method call or
as part of creating a new instance using the new expression) and no
definition of the class could be found.
The searched-for class definition existed when the currently executing class was compiled,
but the definition can no longer be found.
That means that NoClassDefFoundError can be thrown when that particular class is present during compile time but somehow not available during runtime. This could be due to missing JAR file, permission issues, or incorrect classpath on runtime.
Normally I see these issues when developers neglect to define the classpath for used libraries. They forget that your IDE has its own file defining the classpath (i.e. Eclipse has the .classpath file) so running the application from the IDE works fine (class is present during compile time), but after the application is compiled and the classpath is not defined in the machine hosting the application, NoClassDefFoundError is thrown (class "missing" at runtime).
My suggestion is figured out first if the classpath is correct. More times than none, this is the issue. If the classpath is correct, make sure all permissions are set correctly.

NoClassDefFoundError by switching to another project

I have read a lot of answers here to NoClassDefFoundError, but somehow I could not find one that solves my problem. Maybe I have not enough experience to transform it to my problem.
Anyway, I have a project (imported over git) with a package and lots of helpful functions. There is as well a package with some demos, they work completely fine.
If I now copy one of those demos in my own project, I get the error
Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/data/xy/XYDataset
at net.[...]
I think there must be a simple solution, since it runs in the other project.
Thank you for taking your time.
NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time. For example, if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw NoClassDefFoundError.
The class is not available in Java Classpath.
You might be running your program using jar command and the class was not
defined in manifest file's ClassPath attribute.
Any start-up script is overriding Classpath environment variable.
Because NoClassDefFoundError is a subclass of java.lang.LinkageError
it can also come if one of its dependency like native library may not available.
If you are working in J2EE environment than the visibility of Class
among multiple Classloader can also cause
java.lang.NoClassDefFoundError, see examples and scenario section for detailed discussion.

Exception in thread “main” java.lang.NoClassDefFoundError: javax/ws/rs/core/Response

I'm trying to run a jersey client and facing this issue.
This problem gets out when i try to run the class on command prompt. While if i run it on intellij it works fine. Can anybody help me with that? Ps: i'm a beginner in java
#Anja.M we need more details in order to be able to help you. The most important thing when it comes to a java application is to create the class path correctly. I believe this is stated above. You need describe all this dependensies into the java command when you are starting the application. Jersey depends on libraries as well the class that is throwing your exception is contained within javax.ws.rs-api-2.0.1.jar. This file contains the missing javax/ws/rs/core/Response This file you need to include in the class path the following way:
java -classpath classpath1;classpath2...
Let me summarize. You have two type of dependencies one type is inside the /api folder when you download the jersey bundle. The second one is located under -lib
You need to make sure that /api content and the lib is in your class path. Maybe not all jar files are needed for the client side , but you can find out later. As a start include them all.

Trying oracle JavaFX example but getting error

http://docs.oracle.com/javafx/2/text/NeonSign.java.html
when I try to run the neonsign java example from oracle in intelliJ this is my error:
Exception in thread "main" java.lang.ClassNotFoundException: NeonSign
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
https://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html
public class ClassNotFoundException
extends ReflectiveOperationException
Thrown when an application tries to load in a class through its string name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader .
The loadClass method in class ClassLoader.
but no definition for the class with the specified name could be found.
As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The "optional exception that was raised while loading the class" that may be provided at construction time and accessed via the getException() method is now known as the cause, and may be accessed via the Throwable.getCause() method, as well as the aforementioned "legacy method."
I believe this usually happens when you do not have the Library/jar in question added, and have to add it to your current project.
Please make sure you are Using Java 8 (1.8) or you have JavaFX added to your project.
In this case, after looking through the example I found this line of code
scene.getStylesheets().add(NeonSign.class.getResource("brickStyle.css").toExternalForm());
I wonder if the issue is because it's trying to get a resource that doesn't exist (brickStyle.css). If you remove this line, does the error persist?
This might also have to do with you not having JavaFX in your project.
Also, additional reading.
How do I resolve this Java Class not found exception?
Just to add, depending on if IDEA gives you a sample of an FX application, but Netbeans has a FXApplication project with "FXMainClass" that has a built in small example.
Good luck.

Categories