• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Is it possible to reload a class when application is running?

 
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thank you for reading my post
I am developing a swing application and I am looking for a solution to update some classes without closing the application.

for example I have 5 singletone class and I want to be abl to update them (by downloading the class from the internet or reading it from a file...) without closing and running the application again.


thanks.
 
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't muck about with complicated things like reloading classes. Just provide those classes with a "reset" method that sets their state back to the way it is when they are initialized.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiyall,
i was mucking about with an on the fly compiler ( java that modifies itself ) and have found that if the class has not been used ( called ) then you can modify it / replace it then call it. Otherwise if the class has been 'used' the class is loaded into memory and you can change the class all you want on the disk but it wont happen unless you restart the app and call that class ... doh. ( this would seem to be a security issue ).

However have you tried a class loader .. I use this to load the compiled classes that my app modifies then execute them. eg ...

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;



public class cLoader extends URLClassLoader
{
protected cLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}

public Class loadClass(String name) throws ClassNotFoundException {
System.out.println("loadClass: " + name);
return super.loadClass(name);
}

protected Class findClass(String name) throws ClassNotFoundException {
Class clas = super.findClass(name);
System.out.println("findclass: loaded " + name +
" from this loader");
return clas;
}
}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[PM]: i was mucking about with an on the fly compiler ( java that modifies itself ) and have found that if the class has not been used ( called ) then you can modify it / replace it then call it. Otherwise if the class has been 'used' the class is loaded into memory and you can change the class all you want on the disk but it wont happen unless you restart the app and call that class ... doh. ( this would seem to be a security issue ).

It is possible to reload a class that's been used - but you need to use a different classloader than the one that loaded it the first time. And the new classloader cannot have the previous classloader as a parent or other ancestor, or the ancestor's definition will be used while the new one is ignored. (Class loaders are very respectful of their elders.)

If you want to allow the original class definition to be unloaded, you need to allow the original classloader to be reclaimed by GC. Otherwise that original loader will keep a link to the Class object for the original definition, and that will prevent unloading. Of course you also need to make sure there are no more links to instances of the class you want unloaded. And even then, you can never guarantee that unloading will actually occur. So if the original poster wants to use this for singletons, technically there's no way to guarantee that the earlier class definition is ever actually gone. But it may be enough to write code so that whether the old class is still in the JVM or not, the new one is the one actually used for new operations.
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by paul moody:
However have you tried a class loader .. I use this to load the compiled classes that my app modifies then execute them. eg ...


Unfortunately this will not work with the same class loader, as class loaders cache the classes they have loaded.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:

Unfortunately this will not work with the same class loader, as class loaders cache the classes they have loaded.



Infact i could not find what this classloader does that the URLClassLoader does not apart from printing debug info!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic