Servlet giving error java.lang.NoClassDefFoundError - java

I am using the following code in a servlet of my app
java.awt.Image awtImg = java.awt.Toolkit.getDefaultToolkit().createImage(str1);
When I run the application and call the servlet I get the following error
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11.XToolkit
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:169)
java.awt.Toolkit$2.run(Toolkit.java:834)
java.security.AccessController.doPrivileged(Native Method)
java.awt.Toolkit.getDefaultToolkit(Toolkit.java:826)
noticeandreports.pdf.appendFiles.PdfFunctionsClass.addSealSpace(PdfFunctionsClass.java:198)
OJ.NoticesandReports.generate_151_OJNotice.execute(generate_151_OJNotice.java:768)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I have hosted the app on a Linux machine with Java version JDK 1.6.20..
What might be causing the issue...
noticeandreports.pdf.appendFiles.PdfFunctionsClass is the class where the code is written and OJ.NoticesandReports.generate_151_OJNotice is the servlet that calls the method inside the above class...

To use AWT classes in a server side application, I believe you need to run in "headless" mode. Change your servlet container's startup to include:
-Djava.awt.headless=true
(Or set the system property within your own code if you really have to.)
You might also want to consider using an alternative imaging library - either a third-party one or the javax.imageio package.

That is almost certainly not the complete stack trace. Either that stack trace or an earlier one in the log file will tell you what caused the initialization of sun.awt.X11.XToolkit to fail.
However, I'd hazard a guess that the root cause is that the JVM running the web countainer is "headless"; i.e. it doesn't have an accessible display.
The Oracle Java Technical Article entitled "Using Headless Mode in the Java SE Platform" (by Artem Ananiev and Alla Redko, June 2006) describes the issue and what to do about it.
The solution is probably as simple as adding -Djava.awt.headless=true to the JVM options in the web container startup script. For instance, if you are using Tomcat, add that to the $JAVA_OPTS environment variable before calling catalina.sh.

Related

How to debug java.lang.ClassNotFoundException during Tomcat startup?

When Tomcat loads my war, I get this error:
Exception in thread "ClientMessageReceptor0" java.lang.NoClassDefFoundError: java/net/Inet6Address
at org.jacorb.orb.iiop.IIOPAddress.getNetworkInetAddresses(IIOPAddress.java:675)
at org.jacorb.orb.iiop.IIOPAddress.getLocalHost(IIOPAddress.java:591)
at org.jacorb.orb.iiop.IIOPAddress.getLocalHostAddress(IIOPAddress.java:574)
at org.jacorb.orb.listener.SSLListenerUtil.processException(SSLListenerUtil.java:73)
at org.jacorb.orb.iiop.IIOPConnection.handleCommFailure(IIOPConnection.java:77)
at org.jacorb.orb.etf.StreamConnectionBase.read(StreamConnectionBase.java:139)
at org.jacorb.orb.giop.GIOPConnection.getMessage(GIOPConnection.java:389)
at org.jacorb.orb.giop.GIOPConnection.receiveMessagesLoop(GIOPConnection.java:541)
at org.jacorb.orb.giop.GIOPConnection.receiveMessages(GIOPConnection.java:533)
at org.jacorb.orb.giop.MessageReceptor.doWork(MessageReceptor.java:69)
at org.jacorb.util.threadpool.ConsumerTie.run(ConsumerTie.java:60)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: Illegal access: this web application instance has been stopped already. Could not load [java.net.Inet6Address]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForClassLoading(WebappClassLoaderBase.java:1375)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1226)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188)
Our application uses Tomcat v9.0.41 and JDK 1.8.0_111 . This same combination works for others in my team and also it worked for me sometime back. I'm not sure what changed now. I have looked at other similar posts and they suggest mismatch between Tomcat and JDK version. But above combination of Tomcat and JDK works for others.
I have removed all other JDK and JRE version on my system.
Ensured JAVA_HOME points to JDK 1.8.0_111 and PATH points to correct JRE.
Any pointers on how to debug this further? Can I turn on some logging during Tomcat startup to see what paths are being used by Tomcat? Any suggestions are highly appreciated :).

Eclipse and Tomcat 8.0.35 app ERROR: Illegal access: this web application instance has been stopped already

I'm running a Java app in Eclipse EE Oxygen 32 bit in a 64 machine since the program will be transferred to a 32 bit machine later. The app is connected to mysql installed through Wampserver, and the server I'm using in the app is Apache Tomcat 8.0.35. When I run the app in "Run as> Run on Server" inside Eclispe. I get the following error message in "Console":
Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1372)
at org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1042)
at com.mysql.jdbc.AbandonedConnectionCleanupThread.checkContextClassLoaders(AbandonedConnectionCleanupThread.java:90)
at com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:63)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
How can I debug that?
The java code is totally portable across 32-bit and 64-bit platforms, doesn't make sense to use a 32 bit Eclipse version.
To solve your issue, just restart the Tomcat server to get rid of the older cache then try again.
If still doesn't work:
Restart Eclipse
Clean up Eclipse working directory (see #nitind comment).
Run Eclipse in clean mode (see this)

Tomcat 7 can't find my class throws javax.el.ELException: java.lang.NoClassDefFoundError

My web app runs tomcat 7, myfaces, primefaces.
I have a problem on my production m/c that I am not able to replicate on development environment even though the exact code and libraries are loaded on both the machines. The only difference is the tomcat subversion, dev has tomcat7.0.35 and prod has tomcat7.0.40
javax.servlet.ServletException: javax.el.ELException: java.lang.NoClassDefFoundError: Could not initialize class com.myapps.util.UIStringUtil
javax.faces.webapp.FacesServlet.service(FacesServlet.java:229)
com.myapps.util.LoginFilter.doFilter(LoginFilter.java:56) **root cause** java.lang.NoClassDefFoundError: Could not initialize class com.myapps.util.UIStringUtil
com.myapps.bean.UserAuthBean.login(UserAuthBean.java:106)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.apache.el.parser.AstValue.invoke(AstValue.java:278)
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
org.apache.myfaces.view.facelets.el.ContextAwareTagMethodExpression.invoke(ContextAwareTagMethodExpression.java:96)
org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:68)
javax.faces.component.UICommand.broadcast(UICommand.java:120)
javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:1028)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:286)
javax.faces.component.UIViewRoot._process(UIViewRoot.java:1375)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:38)
org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:170)
org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)
com.myapps.util.LoginFilter.doFilter(LoginFilter.java:56)
I am not sure why this is happening. I am curious why this is being thrown as a javax.el.Elexception, where was this is being thrown from with in java bean code. The exact line of code where this is happening is (UserAuthBean.java:106)
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
UIStringUtil.getUIString("msgKey"),
UIStringUtil.getUIString("msgKey")));
UIStringUtil class has static variables and methods that help read data from resource bundles.
As I mentioned earlier, this is only happening on the production box and we are not able to reproduce it on the development box.
Please provide some help here.
It's thrown as an ELException because it happened during executing some EL expression.
You should be looking at the root cause of the exception to learn about the root cause of the exception. Look further down in the stack trace to find the root cause. The first is already present in the incomplete stacktrace you posted:
java.lang.NoClassDefFoundError: Could not initialize class com.myapps.util.UIStringUtil
This basically means that loading of the class as done by Java under the covers as follows
Class.forName("com.myapps.util.UIStringUtil");
has failed with an exception. If you're absolutely positive that the class is present in the runtime classpath, then that can only mean that any of the static variables or initializer blocks of the class has thrown an exception. Loading a class namely initializes all static variables and executes all static initilaizer blocks (haven't you ever wondered how those JDBC drivers work?).
That exception should in turn be visible as the bottommost root cause in the stack trace, after the java.lang.NoClassDefFoundError cause. Perhaps a rather self-explaining java.lang.NullPointerException?

What this following error means?

I am running my java program but on executing it gives me following error.
before it was running fine but now it's throwing following error.
I checked my class path, path in environment variable all are correct.
Exception in thread "main" java.lang.UnsatisfiedLinkError: java.util.zip.ZipFile
.open(Ljava/lang/String;IJ)J
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:114)
at java.util.jar.JarFile.<init>(JarFile.java:135)
at java.util.jar.JarFile.<init>(JarFile.java:72)
at sun.misc.URLClassPath$JarLoader.getJarFile(URLClassPath.java:646)
at sun.misc.URLClassPath$JarLoader.access$600(URLClassPath.java:540)
at sun.misc.URLClassPath$JarLoader$1.run(URLClassPath.java:607)
at java.security.AccessController.doPrivileged(Native Method)
at sun.misc.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:599)
at sun.misc.URLClassPath$JarLoader.<init>(URLClassPath.java:583)
at sun.misc.URLClassPath$3.run(URLClassPath.java:333)
at java.security.AccessController.doPrivileged(Native Method)
at sun.misc.URLClassPath.getLoader(URLClassPath.java:322)
at sun.misc.URLClassPath.getLoader(URLClassPath.java:299)
at sun.misc.URLClassPath.getResource(URLClassPath.java:168)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: com.sun.tools.javac.Main. Program will exit.
To elaborate on #Peter Lawrey's answer ...
The start of the stacktrace is this:
Exception in thread "main" java.lang.UnsatisfiedLinkError: java.util.zip.ZipFile
.open(Ljava/lang/String;IJ)J
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:114)
...
The UnsatisfiedLinkError is thrown when you attempt to call a native method that has not been resolved to a method in the corresponding native library. The rest of the message tells us that the method at fault has the signature:
long java.util.zip.ZipFile.open(String, int, long)
and that meshes with the top frame of the stack trace ... and the fact that Java's ZipFile code is known to use a native library for the heavy lifting.
The fact that it has gotten this far means that the JVM has found the native library and loaded it. But apparently, the load didn't resolve this overload of the native open method. That can only mean one thing: that the version of the ZipFile class on the bootclasspath does not match the native library.
We cannot make any definite conclusions about whether this is a JDK or a JRE, but it seems likely that it is a JDK ... unless the OP is trying to call the Java compiler in a strange way. (The "could not find the main class: com.sun.tools.javac.Main" message probably means that the JVM could not load the class ... because of the UnsatisfiedLinkError breakage.)
Either way, JDK versus JRE is not the immediate problem. The real problem is a mismatch between the "rt.jar" on the JVM's bootclasspath, and the native libraries.
The question asks:
how to solve this then?
It depends on what exactly you did to get this error.
What command did you run?
What were the command line options and arguments?
Have you been "messing around" with your JRE / JDK installation?
Are you trying to use the "rt.jar" file from one installation in another one?
It means your rt.jar is for a different version of Java as your JVM.
I would ensure that you don't have a rt.jar in your boot class path and your JRE is installed correctly.
Could not find the main class: com.sun.tools.javac.Main. Program will exit.
When a class fails to load due to some low level error, it reports that the class could not be found.
The reference in the error message about not finding com.sun.tools.javac.Main leads me to believe that this is a program that needs to be run with the JDK rather than just a JRE.
your JRE path is pointing to the JDK path
Solution :
Step 1: Right click the server
Step 2: Click the Run Time Configuration
Step 3: GIVE the JRE path under the JDK.
Solution fixed

Why is tomcat looking for JSF classes?

I am trying to get my development environment up and running, and I am having trouble with Tomcat trying to load JSF classes for some reason. My application does not use JSF; in fact, I haven't even deployed my application to tomcat yet. I am getting a number of stack traces in the startup logs and I cannot load the default tomcat homepage when I try to open http://localhost:8080 (I just get the tomcat 404 page).
Here is what the first stack trace is:
SEVERE: Error configuring application listener of class com.sun.faces.util.ReflectionUtils$ReflectionUtilsListener
java.lang.ClassNotFoundException: com.sun.faces.util.ReflectionUtils$ReflectionUtilsListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1386)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1232)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3712)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1150)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
I also get similar stack traces for the following other classes:
com.sun.faces.config.ConfigureListener
com.sun.faces.application.WebappLifecycleListener
I'm not sure if this is relevant, but I am running Tomcat 5.5.27 with the 1.4 compatibility pack installed, with Java 1.4.2, on Mas OS 10.5.
Thanks for any ideas!
EDIT: It seems that each of the default applications that come with Tomcat (host-manager, balancer, tomcat-docs, jsp-examples, etc) was relying on these JSF classes. I removed these default applications, and everything seems to be working.
So, my question is now:
Why does tomcat come with applications that don't include their dependencies, and what do I need to do to make those default applicaitons work?
Is your tomcat installation totally clean, or is it one you have inherited?
Check if the webapps contains existing wars/directories which may be referencing JSF classes?
UPDATE: Ah -- I see you found this was the case :-)
Not sure why tomcat doesn't include all its dependencies. Perhaps you downloaded a developer release instead of a stable one?
You might have to clean the Tomcat work directory.

Categories

Resources