why am i getting the noclassdeffounderror after deploying? - java

I am getting noclassdeffounderror after deploying the jar with changes to a certain class file.
I am able to see the class file in the jar when i extracted the jar file through the 7zip.
I also de-compiled the .class file to find any errors but did not get any thing .
While deploying i Just dragged the .class file from the eclipse work space into a pre existing jar file. does that matter?
Does the jdk version in which the jar file is compiled matter because i am running on jre 1.7 in eclipse but my deployment environment is of jre 1.6 ?

The problem in my mind is that the class added to the jar has been compiled under a different version of Java. I would guess that you've compiled at 1.7 and 12c is running 1.7, whereas 11g is running 1.6. Either compile the class in 1.6 and add it to the jar or recompile the jar in 1.7 and ensure the environment you're running on has 1.7 or above.

A NoClassDefFoundError states that the class was not available during runtime. Make sure you have it in class path or the static initializer (if present) is not breaking. For the later, you will see an ExceptionInInitializerError somewhere below in the stacktrace.
Not too sure about if jdk version matters or not.

I am getting noclassdeffounderror after deploying the jar with changes
to a certain class file.
This is caused when there is a class file that your code depends on, and it is present at compile time but not found at runtime (see this answer).
While deploying i Just dragged the .class file from the eclipse work
space into a pre existing jar file. does that matter?
Yes, it matters. Be sure to put the class file back in the correct package folder. For example, a class in package com.foo must be in the folder com/foo inside the jar file. It's possible your altered version landed somewhere else when you dragged it into the jar file.
Does the jdk version in which the jar file is compiled matter because
i am running on jre 1.7 in eclipse but my deployment environment is of
jre 1.6 ?
The jar file itself is just a zip file, so no, it doesn't matter which version of Java you were using to compress the contents of the jar file.
However, if you've re-compiled a single class from an existing jar file, it does matter which version of Java was used to compile the other classes in the jar file, because you could cause the class to be incompatible with its companion classes.

This worked perfectly when i compiled the code in jdk 1.6 . java.security.SecureClassLoader is changed in both the versions. thats why i suppose i got the error .

Related

Java ClassVersionError although version seems to be okay

I'm getting the following error in my java web app (The application launches okay but when I click a button following error occurs). I'm using Tomcat (7.0.109) to run this application.
java.lang.UnsupportedClassVersionError: FileDetailsServlet has been compiled by a more recent version of the Java Runtime (class file version 59.0), this version of the Java Runtime only recognizes class file versions up to 52.0 (unable to load class [FileDetailsServlet])
Previously I used Java 15 (class version 59.0). But then I switched to JDK and JRE 1.8.0_311 (class version upto 52.0)because of other 3rd party apps.
Here are some things that I did
I've checked class version of FileDetailsServlet class using this
javap -verbose FileDetailsServlet| findstr "major"
command. And it shows that version is 51.0 (Java 7).
I've set JDK compliance to 1.8 in eclipse. (Project specific settings)
After changing JDK I recompiled all the files using JDK 1.8.0_311
I've checked environment variables. JAVA_HOME is "C:\Program Files\Java\jdk1.8.0_311"
JRE_HOME is "C:\Program Files\Java\jre1.8.0_311" and Path is "C:\Program
Files\Java\jdk1.8.0_311\bin"
Am I missing anything? I can not go back to Java 15. So is there any way around?
Check your class path for any "leftovers" from the old system. It looks like you are not inspecting the correct files. Maybe you have the FileDetailsServlet in a wrong version as depencency somewhere, or just in a "standalone" JAR- or WAR-file.
It is also important that Eclipse settings alone will maybe not cover, how the used container serves the servlet, so which technology do you use to run the FileDetailsServlet? That's the crucial point: this thing that you use to run (Tomcat, Jetty, other container, maybe included in a fat WAR or fat JAR file from Sprint Boot or another similar technology) has to use the correct .class file.
As you are using Tomcat as container:
You should check the class files inside the folder webapps/appname folder below your tomcat folder (replace appname with the deployment context name) Take care: when you run tomcat from Eclipse this could be somewhere completely different than the installation folder on disk, but I don't have an Eclipse installation ready to check that.

java.lang.NoClassDefFoundError when running jdk 1.7 compiled jarfile inside project compiled with 1.6 jdk

I'm getting a really weird error (for me). We'd added a new dependency to the project's pom file. The jar file are in the project (downloaded from the repository) and we can see all all the class files and the source code.
We added some code in our program to create some classes defined in the new jar file. The code compiles without errors and the war file deploy in the webserver (websphere) without problems too.
But when debugging the program we found an java.lang.NoClassDefFoundError exception when we try to create the first object from the new jar file.
Today I found that the jar file have been compiled with jdk 1.7 and all our projects is compiled using 1.6 jdk.
I see the jdk used to compile the jar file in the manifest file. Is this info correct ?
The difference between jdks used in jar file and my project could cause the problem ?
I checked that the jar file definition exists in classpath and websphere server was rebooted several times.
Stacktrace :
Caused by: java.lang.ClassNotFoundException: cnaf.verificateurbiciban.ws.v1.VerificateurbicibanV1ServiceCnaf
at java.net.URLClassLoader.findClass(URLClassLoader.java:423)
at com.ibm.ws.bootstrap.ExtClassLoader.findClass(ExtClassLoader.java:191)
at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
at com.ibm.ws.bootstrap.ExtClassLoader.loadClass(ExtClassLoader.java:111)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
at com.ibm.ws.classloader.ProtectionClassLoader.loadClass(ProtectionClassLoader.java:62)
at com.ibm.ws.classloader.ProtectionClassLoader.loadClass(ProtectionClassLoader.java:58)
at com.ibm.ws.classloader.CompoundClassLoader.loadClass(CompoundClassLoader.java:510)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
at com.ibm.ws.classloader.CompoundClassLoader.loadClass(CompoundClassLoader.java:542)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
at com.ibm.ws.classloader.CompoundClassLoader.loadClass(CompoundClassLoader.java:542)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
... 56 more
Problem might be cause by JRE version you are using to execute your code: if you are using JRE 1.6 it would not execute bytecode compiled for JRE 1.7. JRE 1.7 would perfectly run both 1.6 and 1.7 bytecode. Try to switch to JRE 1.7 or higher.

Jar before jre - java classpath

is there a way to run java and tell it to use a certain jar higher in the classpath than the jre?
I know it is possible in eclipse run configuration, but I want it to work using java from command line.
The reason is that there is a class that is loaded from the jre and I wanted a different version that should have been loaded from an external jar. I saw this was the case using -verbose.
Thank you
try this Java arg
-Xbootclasspath/p:my.jar

JAR file does not load class files from ext

I installed JDK 1.6 on my Linux system, the $JAVA _HOME directory is /usr/java/jdk1.6.0_07.
I built the path on Eclipse to $JAVA_HOME. It runs smoothly through Eclipse and loads all third party JAR files from /usr/java/jdk1.6.0_07/jre/lib/ext/, but when I export the JAR file and run it, it throws ClassNotFoundExecption.
Why?
Did you install Java properly? Here are some instruction for installing Java 7 or Java 6
Are you sure that the version of Java you are using is correct one since there can be several versions of java on linux? Try java -version on terminal where you run it to check.
Do you use any third party library? If so, did you specify the class path when you run the jar file or bundle them inside your jar file?
What does java -version return? Are you using the same JRE for execution?

JDK 1.6 compiled code call a JDK 1.5 compiled code. what errors will we see?

If a class compiled with JDK 1.6 calls another class compiled with JDK 1.5, what kind of errors will we see?
Class Not found exception?
Class cast exception?
NoSuch method exception?
You should encounter no problems doing this. I have a code library with packages compiled with everything from 1.2 to 1.4 and have had absolutely no problems using it with applications compiled with Java 5 and Java 6.
I was getting the error 'Caused by: java.lang.NoClassDefFoundError:" file name
Check for the file whether deploy in correct path in server or not.
If deploy correctly and still not picking then check the environment java version and Eclipse java version.Both should be matched.
If not matching then took the file from environment and recompile the java file in eclipse by setting the Eclipse compilation configuration changing to the Environment java version.
Deploy it in server and Enjoy.

Categories