• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reloading a new version of a class  RSS feed

 
Gary Richards
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've been trying to work out a way to have some kind of dynamically reloadable plugin classes in a Java program that i'm writing. However, i've become stuck. What I basically need to know is if there is a way, whilst an application is running, to recompile a class that's already been used by your app, and then make the app use the newly compiled version. At the moment, if I try to do this, my app simply uses the same version as it had used originally. If I exit the application and start it again, then it uses the newly compiled version. I think I understand why this is happening (although a definitive answer would be useful) and i'd like to know if it's at all possible to do what i'm trying to do here. If it's not, then does anyone else have any ideas on how I could make some kind of dynamically reloadable plugin classes?
I hope i've explained that well enough
Thanks
Gary
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to play around with ClassLoader and may have to write your own.
 
Gary Richards
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've just been playing around with this some more. I have my own ClassLoader now called PluginClassLoader.
However, when simply using the same instance of my PluginClassLoader to load the class a second time I get:
Exception in thread "main" java.lang.LinkageError: duplicate class definition: Test
Which again, I think makes sense.
However, If I create a second instance of my PluginClassLoader and then use that to load the second instance of my class, it seems to work fine and loads the newly compiled version.
Now, to me this seems like a pretty bad idea. As I could end up having a number of PluginClassLoaders sitting around doing nothing once the new plugin has loaded. Or would the garbage collector sort these out for me? My idea now is that for every plugin I load, I create a PluginClassLoader for. If I load 5 plugins, I end up with 5 different PluginClassLoaders. Then, if I reload one of my plugins, I stop the first instance of the plugin and remove all references I have too it, I then create an extra PluginClassLoader, and start the new plugin from that. This should then leave the original instance of the plugin and the no longer needed PluginClassLoader ready for garbage collection. Is there anything i've overlooked in relation to doing this?
Thanks
Gary
[ May 05, 2004: Message edited by: Gary Richards ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, you've discovered the rule for class unloading: a class will never be unloaded until the ClassLoader that loaded it is discarded.
You're doing the right thing. Following are some good resources for learning the nuances of class loading in Java 2:
http://www.neward.net/ted/Papers/index.html (some really excellent papers!)
http://www.javaworld.com/javaworld/jw-03-2000/jw-03-classload-p2.html
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JUnit manages to reload classes if you run the GUI. You can crib some source code from them. I think their class loader looks exactly like the articles you'll find on the subject.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!