How to allow the classloader to load classes from a changed jar?

可紊 提交于 2020-01-30 11:35:27

问题


We have a server side Java6 app running on quite a few linux servers. Occasionally, someone will accidentally upgrade the jar file while the application is running.

When that happens, the next attempt to load a class that hasn't been used yet (often the ShutdownHandler code) fails with a ClassNotFoundException.

I would like to tell the classloader that it is ok to read the changed jar file to get the classes it needs. I don't mind if this results in the classloader re-reading already loaded classes, although that isn't required.


回答1:


You must create your own class loader for this, and the jar must be outside of the classpath in order not to interfere with the existing class loaders.

When it is detected that the jar has changed (look at the file stamp ever X seconds) then tell your application to stop, discard the old classloader, create a new class loader, and tell your application to start inside the new classloader.

You must do it like this, or instanceof with friends will not work correctly. (If you want to learn more about this, look into all the fine details of JNDI).

Your EASIEST way to do this, would be to write it as a web application (WAR) and deploy it to a servlet container where the things you need to do is done as part of the setup and tear down of the web application. You do not need the servlet container to have a web server presense (http port listener etc).

Most modern servlet containers allow for automatic redeployment. A good, small one is Jetty.




回答2:


The problem is likely to be that the original file which was opened has been deleted. (and replaced) While the new jar might have the same file name, its not the original file. You should be able to customise a ClassLoader to allow this, but unless your application server supports this already, its unlikely to work.

The real answer is to not change an application while it is running.



来源:https://stackoverflow.com/questions/7351499/how-to-allow-the-classloader-to-load-classes-from-a-changed-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!