• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can I "hot-swap" a .class file in a running application?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a running Web app, if I want to edit a particular class, can I just replace the original class file with a edited and then recompiled class file? (assume I have not edited any of the method signature or class member variable. I just edit the programming logic inside methods).
If no, do I need to,
1. restart the application?
2. recompile and replace all classes that use this particular class, and then restart the application?
3. recompile the whole application, redeploy it and restart the application?
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A short answer is that once a Class gets referred to in an application, it is cached and you need to resart before any changes to the Class will be seen.
Things are slightly more interesting for web applications, since it is sometimes possible to see canges to the Class by restarting to web application rather than the entire application server - it depends where the Class is 'seen'. If the Class exists entirely in the web application (ie it is only in either the web-inf/classes or web-inf/lib directory) then altering it here and restarting the web application should be enough.
Deployment is the next step up and depends on your application server and development environment. If your Class is deployed in an EAR, you may need to restart the EAR rather than the web application. It is also possible to make Classes visible to the application server, in which case you will always need to to restart the application server to uncache and reload Class changes.
You can always write code to manually reload classes in a similar manner to restarting a web-app, but this is almost always more trouble than it is worth unless you really need it.
Dave
 
Henry Leung
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx for your reply. In fact, my situation is, I have deployed my web app onto the productin server. And then a problem that had never happened on the testing enviroment happens. I want to modify the program, a particular class for retriving DB connection, but I have no right to restart the app server. So I wonder if there is other way round.
I know that once an application starts up, classloader will begin to load classes into JVM. And I am quite sure that particular class has already been loaded. I understand that once a class is loaded into JVM, it's no use to modify the .class file. But I just see if, and hope that, I am wrong.
I am using a JRun 3.0 server. It's not following J2EE completely. You cannot restart an application alone. You have to restart the whole app server instance.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
an extremely ugly solution I once used (I claim I was forced into it ) was to have a URLClassLoader in a JSP that loaded a jar and used the code in that JAR. To dynamically reload the jar, I just had to modify the JSP. This caused the JSP to be unloaded and freed the URLClassloader holding the jar thus unloading the code, ready to be reloaded when the JSP recreates the URLClassLoader.
I'm not recommending you do this, particularly in production. That's a form of evil even I wouldn't try
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic