Illegal access: this web application instance has been stopped already - java

I have a class which has an init-method defined in xml
<bean id="appStarter" class="com.myapp.myClass" init-method="init" destroy-method="destroy"/>
myClass:
public class myClass{
private Thread t;
public void init() {
t = new Thread() {
#Override
public void run() {
while (true)
try {
doStuff();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
}
public void destroy() {
t.interrupt();
}
}
When the app starts, these threads run fine, and everything works just fine
and after sometime i get the following exception.
INFO: Illegal access: this web application instance has been stopped already. Could not load com.sun.mail.imap.IMAPStore. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1273)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
at javax.mail.Session.getService(Session.java:755)
at javax.mail.Session.getStore(Session.java:569)
at javax.mail.Session.getStore(Session.java:531)
at javax.mail.Session.getStore(Session.java:510)
in the doStuff method:
public void doStuff(){
Session sessioned = Session.getDefaultInstance(System.getProperties(),
null);
Store store = sessioned.getStore("imap");
store.connect(hostName, userName, password);
.
.
.
}
I don't know why, any ideas ?

Problem solved after restarting the tomcat and apache, the tomcat was caching older version of the app.

In short: this happens likely when you are hot-deploying webapps.
For instance, your ide+development server hot-deploys a war again. Threads, that have been created previously are still running. But meanwhile their classloader/context is invalid and faces the IllegalAccessException / IllegalStateException becouse its orgininating webapp (the former runtime-environment) has been redeployed.
So, as states here, a restart does not permanently resolve this issue. Instead, it is better to find/implement a managed Thread Pool, s.th. like this to handle the termination of threads appropriately. In JavaEE you will use these ManagedThreadExeuctorServices. A similar opinion and reference here.
Examples for this are the EvictorThread of Apache Commons Pool, that "cleans" pooled instances according to the pool's configuration (max idle etc.).

I suspect that this occurs after an attempt to undeploy your app. Do you ever kill off that thread that you've initialised during the init() process ? I would do this in the corresponding destroy() method.

Restarting Your Server Can Resolve this problem.
I was getting the same error while Using Dynamic Jasper Reporting , When i deploy my Application for first use to Create Reports, the Report creation works fine, But Once I Do Hot Deployment of some code changes To the Server, I was getting This Error.

If it's a local development tomcat launched from IDE, then restarting this IDE and rebuild project also helps.
Update:
You could also try to find if there is any running tomcat process in the background and kill it.

I had this exact error. My app ran fine inside of the eclipse development environment but when deployed to the production Tomcat server, the app would load fine, then when an endpoint was called, it generated the error:
[Abandoned connection cleanup thread] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load []
The error was caused because there were two different java versions. My development environment in eclipse was set to jdk-15 and the production server was running version 11.
To fix the issue, in eclipse, even when using jdk-15, I emulated java-11:

Related

java.lang.IllegalStateException while reloading Tomcat

I'm working on Spring with Hibernate application, It's working fine but while starting or reloading tomcat server, i'm getting java.lang.IllegalStateException Exception.
Can anybody explain, why this exception occurring and how to resolve it??
INFO: Illegal access: this web application instance has been stopped already. Could not load java.net.BindException. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at com.mysql.jdbc.SQLError.createLinkFailureMessageBasedOnHeuristics(SQLError.java:1220)
at com.mysql.jdbc.exceptions.jdbc4.CommunicationsException.<init>(CommunicationsException.java:57)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3270)
at com.mysql.jdbc.MysqlIO.quit(MysqlIO.java:1659)
at com.mysql.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:4296)
at com.mysql.jdbc.ConnectionImpl.cleanup(ConnectionImpl.java:1265)
at com.mysql.jdbc.ConnectionImpl.finalize(ConnectionImpl.java:2667)
at java.lang.System$2.invokeFinalize(Unknown Source)
at java.lang.ref.Finalizer.runFinalizer(Unknown Source)
at java.lang.ref.Finalizer.access$100(Unknown Source)
I want to give you couple of choices. You can try it. Any option can fulfill your demand.
Restart your tomcat and apache server because a long time of using,
it keeps older version of your application.
Clean your tomcat temp directory and restart
As stated in error,
The eventual following stack trace is caused by an error thrown for
debugging purposes as well as to attempt to terminate the thread which
caused the illegal access, and has no functional impact.
It may be the real cause that at this moment you are in debugging mode and it doesn't clear the running thread. so remove your break point and run it not debugging
If your code contains any kind of thread which are not properly terminated, then this type of error may occur.
That you have init() method but no destroy() method, then this type of error may occur. For details, you can follow the link - http://www.javaspecialists.eu/archive/Issue056.html
if the webapp has stopped, or is stopping, that means that the .war file or WEB-INF/web.xml timestamp changed, and the webapp might be reloading. Please check timestamp is OK or not.
To turn it off, set reloadable="false" in the context definition of
your app. It might be tomcat's server.xml.
In details solution:
The tomcat's server.xml of the reloadable Context is set to false.
For example:
Context path="/expert" docBase="expert" debug="0" reloadable ="false"/>
The solution is easy, as long as the tomcat's server.xml in reloadable = "true" into false on the line, but to do so would lose the advantage of hot deployment, and for the development is not very convenient, simply change it or not. This error does not matter.
Error principle:
The reason is because the tomcat restart, because the previous tomcat thread has not completely shut down, restart tomcat will report this exception, but this does not affect the normal use, just jump abnormal annoying people. Used hibernate, spring or other large components, when a WEB application system has a lot of class,
if you turned the Tomcat reloadable = true, then whenever the relevant documents change, Tomcat stops web app and frees up memory, and then reload web app. it may be a huge project. So we always think if there is only a certain class of overloaded functions, will greatly meet our debugger.
UPDATE: For code related issue
First, I want to tell you that I have given you some solutions for tomcat basis. Now I want to give you a solution for code basis. Would you please cross check your code with this issue? Please follow the URL.
http://www.coderanch.com/t/660126/Wiki/Illegal-State-Exception
UPDATE: For MySQL related issue
There are 2 issues.
This web application instance has been stopped already. Could not load java.net.BindException.
This web application instance has been
stopped already.Could not load com.mysql.jdbc.
This is because the MySQL JDBC driver on the application under the WEB-INF/lib directory, in the re-release of its loaded twice, so long as it can be copied to %TOMCAT_HOME%/lib can solve the problem.
We can solve these two anomalies MySQL drivers from the WEB-INF/lib folder moved to %TOMCAT_HOME%/lib.
I suspect this might be happening after the web application is restarted, where it's down for a short period of time. Then some finalize() method in the code is probably trying to do some cleanup too late. Whether or not that's in your code or the MySQL driver I can't say. You definitely should only have one version of a jar in a directory at a time. You might want to upgrade it to the latest (5.1.38 right now) in case something has been fixed that might be affecting you.(Number 9 is copied from #WhiteFang34)
Related link for 9: tomcat 6.0.24 Exception: Could not load com.mysql.jdbc.SQLError
The issue was because of the server cache. Follow the below steps to fix this issue
Find the directory called "work" under your tomcat's root directory
Delete it completely
Restart the server
Issue will be fixed.
Note: In case you have default tomcat installation in linux machine, you have to delete the folder /var/cache/tomcat7/Catalina
I think that the root cause of this problem is that something is leaking JDBC Connection objects.
These objects are actually instances of com.mysql.jdbc.ConnectionImpl ... which is a class with a finalize(). Normally, object finalization tidies up the leaked connections. In this case, it looks like the sequence of events is as follows:
the GC runs after the webapp has shutdown
a ConnectionImpl object is found to be unreachable
the object's finalize() method attempts to shutdown the MySQL database connection ... which entails sending a message to the server
when the message send fails for some reason, and the comms-level code tries to create an exception by calling com.mysql.jdbc.SQLError.createCommunicationsException
that tries to create an exception reflectively ...
The last step is failing because it is trying to use the webapp's classloader for the webapp ... which has been shut down. Apparently, this is detected by a check that is intended to help programmers diagnose webapps that don't shutdown cleanly.
The best solution is to track down the source of the Connection leak(s) and fix it. If Connection objects did not leak, then they would be closed while the webapp's classloader was still active.
Alternatively, you could simply turn off the check, as described in #Skywalker's answer.
The problem could be a simple SQL Error. tThe
java.lang.IllegalStateException
means that an object or class was called on when it was not ready to be called on. These errors at the bottom of the stack
at java.lang.ref.Finalizer.runFinalizer(Unknown Source)
at java.lang.ref.Finalizer.access$100(Unknown Source)
at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)
look like they come from the Java Garbage Collector
attempting to clean up MySQL Object Pointers
that were temporarily unreferenced during a reboot. It seems to me that your error here is just a few MySQL Object Pointers being halted in the middle of execution when the GC calls their finalize methods and then pauses the application to attempt a clean up the object. (leading to a fail and a throw of this exception)
This error is most likely harmless, and could maybe be fixed by adding a
JNI_DestroyJavaVM();
to your main thread to be executed on shutdown.

Hazelcast keep running after application stops on Weblogic (and also Tomcat)

I have just been playing around to get what Hazelcast provides. There are only a few things using Hazelcast in the applcation.
On Weblogic server 10.3.6, I saw that there is something wrong on one of the Maps. It kept giving exception about no class definiton error even though it has been running maybe more than a few days without a problem. Then I stoppped the application on running 8 nodes on Oracle Weblogic. I assumed the Hazelcast clusters/instances would shutdown as well but even though the application stops on all the nodes of Weblogic, I saw Hazelcast merge exceptions on the logs (keep throwing).
I also test my spring based application on Tomcat 7, even though I shutdown the application, Hazelcast resists shutting down somehow.
Is it normal behaviour? How can we shutdown all Hazelcast instances even after shutting down the application?
Note that I call Hazelcast when my application starts, there is no special client, only 8 nodes of Weblogic server.
Edit: Here is the stacktrace of the migration problem
SEVERE: Problem while reading DataSerializable, namespace: 0, id: 0, class: 'com.hazelcast.partition.impl.MigrationRequestOperation', exception: com.hazelcast.partition.impl.MigrationRequestOperation
com.hazelcast.nio.serialization.HazelcastSerializationException: Problem while reading DataSerializable, namespace: 0, id: 0, class: 'com.hazelcast.partition.impl.MigrationRequestOperation', exception: com.hazelcast.partition.impl.MigrationRequestOperatio
at com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:120)
at com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:39)
at com.hazelcast.nio.serialization.StreamSerializerAdapter.toObject(StreamSerializerAdapter.java:65)
at com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:260)
at com.hazelcast.spi.impl.NodeEngineImpl.toObject(NodeEngineImpl.java:186)
at com.hazelcast.spi.impl.BasicOperationService$OperationPacketHandler.loadOperation(BasicOperationService.java:638)
at com.hazelcast.spi.impl.BasicOperationService$OperationPacketHandler.handle(BasicOperationService.java:621)
at com.hazelcast.spi.impl.BasicOperationService$OperationPacketHandler.access$1500(BasicOperationService.java:614)
at com.hazelcast.spi.impl.BasicOperationService$BasicDispatcherImpl.dispatch(BasicOperationService.java:566)
at com.hazelcast.spi.impl.BasicOperationScheduler$OperationThread.process(BasicOperationScheduler.java:466)
at com.hazelcast.spi.impl.BasicOperationScheduler$OperationThread.processPriorityMessages(BasicOperationScheduler.java:480)
at com.hazelcast.spi.impl.BasicOperationScheduler$OperationThread.doRun(BasicOperationScheduler.java:457)
at com.hazelcast.spi.impl.BasicOperationScheduler$OperationThread.run(BasicOperationScheduler.java:432)
Caused by: java.lang.ClassNotFoundException: com.hazelcast.partition.impl.MigrationRequestOperation
at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:297)
at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:270)
at weblogic.utils.classloaders.ChangeAwareClassLoader.findClass(ChangeAwareClassLoader.java:64)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at weblogic.utils.classloaders.GenericClassLoader.loadClass(GenericClassLoader.java:179)
at weblogic.utils.classloaders.ChangeAwareClassLoader.loadClass(ChangeAwareClassLoader.java:43)
at com.hazelcast.nio.ClassLoaderUtil.tryLoadClass(ClassLoaderUtil.java:124)
at com.hazelcast.nio.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:113)
at com.hazelcast.nio.ClassLoaderUtil.newInstance(ClassLoaderUtil.java:66)
at com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:109)
... 12 more
Mar 16, 2015 5:31:29 PM com.hazelcast.spi.OperationService
The credible Hazelcast docs says:
As a final step, if you are done with your client, you can shut it down as shown below. This will release all the used resources and will close connections to the cluster.
client.shutdown();
which should be called in destroy() method of your bean:
public class ExampleBean implements DisposableBean {
public void destroy() {
client.shutdown();
}
}
The exception you have pasted occurs due to the fact that your app class loader is destroyed upon shutdown of your app.

INFO: Illegal access: this web application instance has been stopped already. Could not load java.net.InetAddress

I'm experiencing this kind of exception, can someone help me about this problem?
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1566)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at org.quartz.utils.UpdateChecker.getClientId(UpdateChecker.java:149)
at org.quartz.utils.UpdateChecker.buildParamsString(UpdateChecker.java:120)
at org.quartz.utils.UpdateChecker.buildUpdateCheckUrl(UpdateChecker.java:114)
at org.quartz.utils.UpdateChecker.doCheck(UpdateChecker.java:55)
at org.quartz.utils.UpdateChecker.checkForUpdate(UpdateChecker.java:47)
at org.quartz.utils.UpdateChecker.run(UpdateChecker.java:39)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
I'm also getting the same exception with Could not load java.net.URLEncoder and Could not load java.net.URLConnection. I'm using Eclipse Indigo SR1 and Tomcat V6.0
You can solve this problem after restarting the server, the server iscaching older version of the app.
see here
You cannot resolve this problem by "restarting the server".
The problem is that when you update your app by uploading a new version to Tomcat, some resource is not cleaned up properly. Could be anything from a runaway thread, to some kind of scheduled service, to a listener. When that process tries to load a class, Tomcat will detect and prevent it.
The solution to the problem is to properly keep track of, and on shutdown close, those threads.
I have also experienced this problem recently.
In my case the problem was generated by an unhandled exception in javax.servlet.ServletContextListener which was logged in one localhost-date.log.
Because of that exception the webapp was not deployed, but an already started java.util.concurrent.Executors was still running which caused another chain of exceptions in catalina-date.log:
java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load WhatEver.class.
The solution for my case:
check all the Tomcat logs
fix exceptions in javax.servlet.ServletContextListener
I have faced the similar issue. I have solved by increasing the permsize of Server in the VM arguments -XX:MaxPermSize=1024m

How to start and stop Tomcat cleanly from Java and why my way doesn't work repeatably?

I am writing a Quartz application that runs on Windows and calls Lucene and Solr to run indexing jobs. It runs a sequence of jobs, and each job consists of these steps:
Make sure Tomcat is stopped (Solr running under Tomcat prevents index dir from being deleted or copied)
Delete old index directory if necessary
Start Tomcat
Make sure Tomcat and Solr app are running
Run the indexing job
Stop Tomcat
Make sure Tomcat is stopped
Copy index directory to an archive
I decided to have the code that starts and stops Tomcat set the system properties that are set in Startup.bat, Shutdown.bat and Catalina.bat, and just call Bootstrap.main with "start" and "stop" parameters. This worked for one iteration, but not when I tried a Quartz run in which I set up two iterations.
When my code shut down Tomcat at the end of the first iteration, all of the usual messages were displayed, including
INFO: Stopping ProtocolHandler ["http-bio-5918"]
(I am using port 5918) but when it tried to start Tomcat at the beginning of the second iteration, it got thes errors:
SEVERE: Failed to initialize end point associated with ProtocolHandler ["http-bio-5918"]
java.net.BindException: Address already in use: JVM_Bind <null>:5918
and
SEVERE: Failed to initialize connector [Connector[HTTP/1.1-5918]]
org.apache.catalina.LifecycleException: Failed to initialize component [Connector[HTTP/1.1-5918]]
I ran netstat -an in a command prompt window, and it confirmed that port 5918 was in use. There's nothing special about the code I am using to check if Tomcat is running. I've seen in various places on the internet.
public boolean isTomcatRunning(String url) {
boolean isRunning = false;
try {
new URL(url).openConnection().connect();
isRunning = true;
} catch (IOException e) {
isRunning = false;
}
return isRunning;
}
but it apparently tells me that Tomcat is not running when it is.
As I said, I am starting and stopping Tomcat by calling Bootstrap.main(new String[]{"start"}) and Bootstrap.main(new String[]{"stop"}). The only thing peculiar about that is that is that when I simply call Bootstrap.main(new String[]{"start"}), it doesn't seem to return (I haven't waited long enough yet to see if it is hanging or just taking a long time), so I have been running it inside a thread.
Maybe that is causing the problem, as it looks like Catalina.bat isn't doing anything special and it returns from startup just fine. I wonder if there is an additional setup I need to do to enable it to run startup in the main thread without hanging.
In any case, this is what I am puzzled about with starting and stopping Tomcat from within my Quartz application, and I would appreciate any help and suggestions you can offer.
I strongly suggest that you wrap your Tomcat instance with a wrapper that controls the lifecycle of your Tomcat instance. Such wrapper is the Java Service Wrapper. An older version (3.2.3 I believe) is "free" and works fine with newer Tomcat instances.
Your controlling application then "talks" to the wrapper to start/stop the Tomcat application. There are multiple benefits with this approach. One of them is that you are not subject to your Tomcat application hanging and the port you are testing not replying anymore.

web instance already stopped

i get this error
Mar 22, 2011 12:36:01 AM org.apache.catalina.loader.WebappClassLoader findResourceInternal
INFO: Illegal access: this web application instance has been stopped already. Could not load META-INF/services/javax.xml.parsers.DocumentBuilderFactory. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
Mar 22, 2011 12:36:01 AM org.apache.catalina.loader.WebappClassLoader loadClass
INFO: Illegal access: this web application instance has been stopped already. Could not load com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1562)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at javax.xml.parsers.FactoryFinder.getProviderClass(Unknown Source)
at javax.xml.parsers.FactoryFinder.newInstance(Unknown Source)
at javax.xml.parsers.FactoryFinder.find(Unknown Source)
at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source)
at com.xmlparser.XmlParser.parseXmlFile(XmlParser.java:33)
at com.xmlparser.XmlParser.<init>(XmlParser.java:25)
at com.jobs.SendRoutineMessagesJob.execute(SendRoutineMessagesJob.java:29)
at org.quartz.core.JobRunShell.run(JobRunShell.java:199)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:546)
could some one please guide me as to what mistake i am committing here. i am not sure if i would need to paste the source code as well.
This because of you are deploying new instance of your application to server but the some thread or connection of previous instance attempts doing some business logic you have written. That's why, the main part of JVM called ClassLoader informs you about "Illegal access: this web application instance has been stopped already" and throws appropriate exception ... You can switch off auto deploy feature of Application Server which preferable on Production Environment and after deploying application restart server manually. Or you can in Class level control Servlet Life Cycle :))) during "pre deploy" close all connection or stop some threads. Or use sophisticated way controlling Threads with ManagedExecutorService. Good luck!!!
seems like i was creating instance of something which was already created..i removed the instance and now it works fine
Are you using Netbeans? I had a similar error except it was could not load oracle.sql.lnxlib. Like many posters have pointed out, this error usually has to do with Tomcat caching multiple instances of the application. A coworker was able to solve the problem for me by undeploying old instances of the application within Netbeans. To do so, go to the Services Tab > Servers and then double click on Apache Tomcat or TomEE and after running the application once since opening netbeans, a Web Applications Folder should appear. If you double click on that, you should see instances of your application appear. You should be able to right click them and then click undeploy (you may have to right click them and hit stop first). Finally, you should stop and then start tomcat before building and running your application.
Hope this helps.
See attached screenshot for clarity
If you use gradle there is an error
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.
You can change mysql-connect-java to
mysql: mysql-connector-java: 8.0.13
Good luck
I got this error on Tomcat on an Ubuntu server ....finally resolved it by doing a Tomcat shutdown with "systemctl stop tomcat" and then "sudo reboot" and then once the box came back up, starting Tomcat again

Categories

Resources