Removing .CLASS files after dynamically compiling using JAVAX

Hi Experts! Here is the problem I have encountered:

My application is written in java and uses JDK 6 to compile a series of strings dynamically using JAVAX.TOOLS.
This process is done every seconds, therefore the class names are as follows:

Where the long number is the millisecond epoch. (The reason the file name & class name changes are because the dynamic code is downloaded from a server and is different every time, otherwise the code will just look for the first compiled code).

Now this makes the application to create thousands of CLASSes in the classes directory and the size of that directory is increased over 400 MBs per 12 hours.

I have tried using regular java FILE.delete method, but since the class is locked from the custom classloader I have, it fails every time.
Also, Its not possible for me to unload the class and then remove, or at least I dont know how.

Your help is highly appreciated.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AFAIK, you can do it using the custom ClassLoader, every .class file which you are generating, load it in a different classloader and as soon as you are done with that class, make the references of that class and it's custom classloader to null and after some time you can try deleting it (If ClassLoader and that class is been garbage collected then your .class will get deleted easily)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bashashAuthor Commented:
Thanks for your reply ernitingoel.
I have managed to explicitly close each class and remove the file.
Here is what I did:
From within the same custom classLoader, (in Finally clause) I re-instantiate the same classLoader, and load the class, it some how releases the lock to the class (but not the classLoader), afterwards I can remove the file using FILE.delete() method. I can leave the old classLoader to the garbage collector. Not sure if thats good practice since the class loader is called every second. But I guess the garbage collector knows its job.
So, I guess im going to have to monitor and see if it really works (efficiently) and then mark this as solved.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.