Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Unloading

 
Shanmuga Raja
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to unload the classes in java?
 
Lin Feng
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recreate your class loader?
 
Ulas Ergin
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Classloader is garbage collected when none of the classes loaded by it is used anymore.And when the classloader is gone,your class is 'unloaded'.

So try creating another classloader and begin loading your classed with it,
derefer the classed loaded by the previous loader and you're done.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gets a little spooky. This code is just typed ... not compiled or tested but I think the basic idea is sound:

Thing one (for cat in hat fans) will be loaded by the default class loader. Thing two will be loaded by a new class loader. If the file thing.class on disk changed since it was first referenced by the default class loader, they might have different versions of the class. That could lead to some very puzzling bugs!

It is done, tho. The JSP container recompiles and reloads JSPs if they have changed on disk. JUnit has an option to load the class under test every time it runs. Some debuggers can reload classes without stopping and restarting a program. But be careful!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic