| Author |
How to dynamically Add Classess to Running VMs class path
|
echarish Kumar
Ranch Hand
Joined: Feb 02, 2010
Posts: 42
|
|
Hi All,
I want to know how can i add classess from a jar through code to the running VM's class path. i want this because a user cn upload a JAR and the classess in that JAR shoud get loded into the SYSTEM Class loader.
I will really appriciate if someone can explain this by a code example.
|
 |
Tim McGuire
Ranch Hand
Joined: Apr 30, 2003
Posts: 819
|
|
This is what Class.forName() is for
check out http://java.sun.com/developer/technicalArticles/ALT/Reflection/
allowing a user to upload any class at all is a huge security problem though.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Not quite Tim. The JAR files need to be added to the system class loader, and that is the tricky part. A URLClassLoader can load JAR files on the fly but it's not the system class loader.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Tim McGuire
Ranch Hand
Joined: Apr 30, 2003
Posts: 819
|
|
Rob Prime wrote:Not quite Tim. The JAR files need to be added to the system class loader, and that is the tricky part. A URLClassLoader can load JAR files on the fly but it's not the system class loader.
missing piece added. Thanks Rob.
|
 |
echarish Kumar
Ranch Hand
Joined: Feb 02, 2010
Posts: 42
|
|
Thanks both of you for your replies , but still i have not got my Answer
|
 |
Vivek K Singh
Ranch Hand
Joined: Dec 22, 2009
Posts: 85
|
|
As rob said, It will be done by the URLClassLoader Check the doc URLClassLoader
|
SCJP 6
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
I just remembered you can use Thread.setContextClassLoader to change the default class loader for a thread.
You may also want to subclass URLClassLoader to make the addURL method public:
|
 |
 |
|
|
subject: How to dynamically Add Classess to Running VMs class path
|
|
|