Could not initialize class Error when loading jcaptcha servlet - java

Everything is running well when debugging my webapp at localhost, but when deploying it to to server (Microsoft Azure, OS:Ubuntu 14.04 tomcat7+apache ), the jcaptcha servlet cannot be found, and it throws Exception as "java.lang.NoClassDefFoundError: Could not initialize class "
type Exception report
message Servlet execution threw an exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause
java.lang.NoClassDefFoundError: Could not initialize class com.tjw.hrmanage.util.jcaptcha.CaptchaServiceSingleton
com.tjw.hrmanage.util.jcaptcha.JcaptchaServlet.doGet(JcaptchaServlet.java:40)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.52 (Ubuntu) logs.

This is because your app in ubuntu can not find the corresponding jar file. You can add this class's jar file to /lib and add reference for this jar in 'build path' in eclipse and then redeploy your project to azure again.

As I known, there are three common reasons that will cause this error.
The class belongs to a missing JAR file or JAR was not added into classpath or sometimes jar's name has been changed.
The class is not in the path CLASSPATH, there is no sure shot way of knowing it but many a times you can just have a look to print System.getProperty("java.classpath") and it will print the classpath from there you can at least get an idea of your actual runtime classpath.
Just try to run with explicitly -classpath option with the classpath you think will work and if it's working then it's a sure short sign that someone is overriding java classpath.
Per my experience, I think your issue matches the first case. Please check your code and the referenced library.

I changed the jdk version from openjdk7 to java-8-oracle and restart my tomcat, then everything is running well, thanks a lot for your answers.

Related

java.lang.NoClassDefFoundError: org/apache/naming/resources/FileDirContext

When i have placed ".war" file in Apache Tomcat/8.5.39 and tried to start server getting the below error
java.lang.NoClassDefFoundError: org/apache/naming/resources/FileDirContext but in previous version tomcat 7.0.34  its working fine
Root Cause
java.lang.NoClassDefFoundError: org/apache/naming/resources/FileDirContext
com.anz.fit.fitas.tomcat.share.StaticContentController.initResources(StaticContentController.java:127)
com.anz.fit.fitas.tomcat.share.StaticContentController.doGet(StaticContentController.java:63)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
org.apache.catalina.servlets.DefaultServlet.service(DefaultServlet.java:418)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
Do i need to add any library or else ?
The org.apache.naming.resources package got removed in Tomcat 8.0 with no direct replacement. There is no alternative library you could add to the classpath to make it work.
Neither the release notes nor the changelog mentions the removal of that package, so I guess the Tomcat authors regarded it is internal.
You should look at your StaticContentController to understand why it has a dependency on on Tomcat's FileDirContext, and replace that dependency with something ideally not dependent on Tomcat internals.

Java webapp produces StackOverflow exception on Linux

I have a Java-web application running on Tomcat 9. It works fine on a Windowns machine. But after deploying this app on a Debian Linux server I've encountered java.lang.StackOverflow exceptions on some particular pages. Here is the beginning of the stacktrace log:
27-Dec-2017 08:54:43.746 SEVERE [https-jsse-nio-9443-exec-3]
org.apache.catalina.core.ApplicationDispatcher.invoke Servlet.service() for
servlet [jsp] threw exception
java.lang.StackOverflowError
at java.io.UnixFileSystem.canonicalize0(Native Method)
at java.io.UnixFileSystem.canonicalize(UnixFileSystem.java:172)
at java.io.File.getCanonicalPath(File.java:618)
at org.apache.catalina.webresources.AbstractFileResourceSet.file(AbstractFileResourceSet.java:90)
Please, see full stacktrace here:
https://pastebin.com/0AmFDY8F
As far as I understand, the exception occurs while compiling the JSP page source by Jasper and it's somehow related to the resolving of Linux paths / directories. Could anybody please help me to figure out what the exact problem is? I didn't create any symbolic links, if the problem can be related to this.
According to your stack trace, the problem is caused by not being able to find a specific path as resolved by your Spring framework:
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
...
Caused by: java.io.IOException: JSPException including path '/struct/context/tutorials.jsp'.
The Spring framework is likely using the canonical paths to resolve the file, and it fails perhaps because there is some quirk in the framework related to the type of Unix system you are deploying to.
Please reference this in the Spring documentation:
https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-install-supported-operating-systems
The default script supports most Linux distributions and is tested on CentOS and Ubuntu. Other platforms, such as OS X and FreeBSD, will require the use of a custom embeddedLaunchScript.
Notice that Debian was not listed. Hopefully this puts you on the right path.
If I'm chasing a red herring here, I'm sorry - but you haven't given us much to go on.
Finally I've found what caused the StackOverflow exception. I have a recursive JSP tag call to render nested options for a <select> tag. After removing the recursion, the exception disappeared. Sure, it's just a workaround, so I'm still wondering why it works fine on Windows and doesn't on Linux.

java.lang.NullPointerException on a local deployment only after file modification

Ok, this is strange. I have WAMP and Tomcat on a win7 workstation. I downloaded a copy of a live website, that is working fine. I also set up a copy of the db. Website is a very old jsp website.
Everything is fine when I work on the homepage. If I click on other pages, like "news", "features", I can see everything working perfectly.
But if I open the news.jsp, features.jsp... files, and I change even the smallest thing, or nothing at all, but I save them again, I get the nefarious nullpointerexception error:
message java.lang.NullPointerException
description The server encountered an internal error that prevented it from fulfilling this request.
exception`enter code here`
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:395)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NullPointerException
org.apache.jsp.Pages.features_jsp.jspDestroy(features_jsp.java:42)
org.apache.jasper.runtime.HttpJspBase.destroy(HttpJspBase.java:59)
org.apache.jasper.servlet.JspServletWrapper.destroy(JspServletWrapper.java:477)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:165)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:368)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
What the heck is going on? How is it possible that the files work fine but if I open them they stop working? Is this a permission problem? I come from php and have no idea on how to deal with this environment.
Have you automatically changed the encoding of file by saving? Check the old encoding and u can compare the size of former and latter files.
Have you automatically changed its line break? Some line break in windows doesn't perform well in Linux, as the well-known \n and \r\n problem. Besides, I once edited a bash file in Notepad++ in widows, but it has exception when executing it in Linux. Then the solution is re-typing it in Linux.
By the way, would you mind pasting the code of jspDestroy(features_jsp.java:42) and so on so I can have more information?
Hope it helps!

Getting NoClassdef on HMAC_SHA1 in Webpshere

We have WAS 6.0 (I know) .2.43 ND running in multiple regions.
Our Dev-B region runs fine, but Dev-C throws a java exception when we make web-calls (at least this is what the developer tells me)...Same code in both regions and I checked the obvious suspects (Global security, SSL ciphers etc) and they all seem to match. Here's the stack trace from SystemErr:
[8/1/12 4:02:31:758 EDT] 0000005c ServletWrappe E SRVE0068E: Could not invoke the service() method on servlet action. Exception thrown : java.lang.NoClassDefFoundError
at javax.crypto.Mac.getInstance(DashoA12275)
at net.oauth.signature.HMAC_SHA1.computeSignature(HMAC_SHA1.java:73)
at net.oauth.signature.HMAC_SHA1.getSignature(HMAC_SHA1.java:39)
at net.oauth.signature.OAuthSignatureMethod.getSignature(OAuthSignatureMethod.java:83)
at net.oauth.signature.OAuthSignatureMethod.sign(OAuthSignatureMethod.java:54)
at com.harcourt.hsp.utils.LTIUtil.generateSignature(LTIUtil.java:62)
at com.harcourt.hsp.web.struts.lti.action.BaseLTIAction.generateSignature(BaseLTIAction.java:238)
at com.harcourt.hsp.web.struts.lti.action.BaseLTIAction.execute(BaseLTIAction.java:96)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1796)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:887)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:90)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1937)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:130)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:434)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:373)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:253)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminaters(NewConnectionInitialReadCallback.java:207)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:109)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java:566)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java:619)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java:952)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java:1039)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1498)
at javax.crypto.Mac.getInstance(DashoA12275)
at net.oauth.signature.HMAC_SHA1.computeSignature(HMAC_SHA1.java:73)
at net.oauth.signature.HMAC_SHA1.getSignature(HMAC_SHA1.java:39)
at net.oauth.signature.OAuthSignatureMethod.getSignature(OAuthSignatureMethod.java:83)
at net.oauth.signature.OAuthSignatureMethod.sign(OAuthSignatureMethod.java:54)
at com.harcourt.hsp.utils.LTIUtil.generateSignature(LTIUtil.java:62)
at com.harcourt.hsp.web.struts.lti.action.BaseLTIAction.generateSignature(BaseLTIAction.java:238)
at com.harcourt.hsp.web.struts.lti.action.BaseLTIAction.execute(BaseLTIAction.java:96)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1796)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:887)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:90)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1937)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:130)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:434)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:373)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:253)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminaters(NewConnectionInitialReadCallback.java:207)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:109)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java:566)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java:619)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java:952)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java:1039)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1498)
Thanks for your help. I'm sure it's a config that I'm missing.
For some reason, javax.crypto.Mac.getInstance is not able to be resolved. If you want to try some low-hanging fruit before full blown troubleshooting: Are you missing a jar? Does the class need to be added to your classpath?
Ultimately, the best way to resolve the issue is a logical step by step analysis. This 3 part article will walk you through troubleshooting the java.lang.NoClassDefFoundError. It's a relatively short read that I suspect will probably answer your question.
For completeness, I've included the short version (pulled from the articles mentioned above):
Review the java.lang.NoClassDefFoundError error and identify the
missing Java class. (In this case javax.crypto.Mac.getInstance)
Verify and locate the missing Java class from your compile / build
environment
Determine if the missing Java class is from your application code,
third part API or even the Java EE container itself. Verify where
the missing JAR file(s) is / are expected to be found
Once found, verify your runtime environment Java classpath for any
typo or missing JAR file(s)
If the problem is triggered from a Java EE application, perform the
same above steps but verify the packaging of your EAR / WAR file for
missing JAR and other library file dependencies such as MANIFEST
Perform a code walkthrough of the affected class and determine if it
contains static initializer code (variables & static block)
Review your server and application logs and determine if any error
originates from the static initializer code
Once confirmed, analyze the code further and determine the root
cause of the initializer code failure. You may need to add some
extra logging along with proper error handling to prevent and better
handle future failures of your static initializer code going forward

"NoClassDefFoundError: Could not initialize class" error

When I run my project, I get numerous outputs of this error:
Sep 9, 2009 8:22:23 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet Jersey threw exception
java.lang.NoClassDefFoundError: Could not initialize class SpringFactory
at com.point2.prospect.persistence.hibernate.HibernateTransactionInterceptor.doFilter(HibernateTrans
actionInterceptor.java:17)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.point2.prospect.restapi.ServerErrorInterceptor.doFilter(ServerErrorInterceptor.java:27)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
I notice that this problem has a pattern of being asked all over the web, but with no real answers. What is a general cause for this kind of error?
NoClassDefFound error is a nebulous error and is often hiding a more serious issue. It is not the same as ClassNotFoundException (which is thrown when the class is just plain not there).
NoClassDefFound may indicate the class is not there, as the javadocs indicate, but it is typically thrown when, after the classloader has loaded the bytes for the class and calls "defineClass" on them. Also carefully check your full stack trace for other clues or possible "cause" Exceptions (though your particular backtrace shows none).
The first place to look when you get a NoClassDefFoundError is in the static bits of your class i.e. any initialization that takes place during the defining of the class. If this fails it will throw a NoClassDefFoundError - it's supposed to throw an ExceptionInInitializerError and indicate the details of the problem but in my experience, these are rare. It will only do the ExceptionInInitializerError the first time it tries to define the class, after that it will just throw NoClassDefFound. So look at earlier logs.
I would thus suggest looking at the code in that HibernateTransactionInterceptor line and seeing what it is requiring. It seems that it is unable to define the class SpringFactory. So maybe check the initialization code in that class, that might help.
If you can debug it, stop it at the last line above (17) and debug into so you can try find the exact line that is causing the exception. Also check higher up in the log, if you very lucky there might be an ExceptionInInitializerError.
You're missing the necessary class definition; typically caused by required JAR not being in classpath.
From J2SE API:
public class NoClassDefFoundError
extends LinkageError
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.
I had faced the same issue, because the jar library was copied by other Linux user(root), and the logged in user(process) did not have sufficient privilege to read the jar file content.
I had this:
class Util {
static boolean isNeverAsync = System.getenv().get("asyncc_exclude_redundancy").equals("yes");
}
you can probably see the problem, the env var might return null instead of string.
So just to test my theory, I changed it to:
class Util {
static boolean isNeverAsync = false;
}
and the problem went away. Too bad that Java can't give you the exact stack trace of the error though, kinda weird.
I recently ran into this error on Windows 10. It turned out that windows was looking for .dll files necessary for my project and couldn't find them because it looks for them in the system path, PATH, rather than the CLASSPATH or -Djava.library.path
I had this same Error and it turns out, I had a method that was being called over and over, like an infinite loop, except it wasn't in an actual loop.
Realised that I was using OpenJDK when I saw this error. Fixed it once I installed the Oracle JDK instead.

Categories