• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to dynamically Add Classess to Running VMs class path

 
echarish Kumar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
echarish Kumar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both of you for your replies , but still i have not got my Answer
 
Vivek K Singh
Ranch Hand
Posts: 85
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As rob said, It will be done by the URLClassLoader Check the doc URLClassLoader

 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic