aspose file tools*
The moose likes Java in General and the fly likes How to dynamically Add Classess to Running VMs class path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to dynamically Add Classess to Running VMs class path" Watch "How to dynamically Add Classess to Running VMs class path" New topic
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: 820

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: 19759
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
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: 19759
    
  20

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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to dynamically Add Classess to Running VMs class path