This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes How to add new jar files to class path dynamically at runtime? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to add new jar files to class path dynamically at runtime?" Watch "How to add new jar files to class path dynamically at runtime?" New topic
Author

How to add new jar files to class path dynamically at runtime?

Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
Not sure this is the right forum for this question, but here goes...

I need to develop a web app (Spring MVC, if you must know, but the question is not spring-specific), that will allow a user to upload a JAR file that they have developed. The servlet needs to scan the JAR for compatible classes that implement a specific "plugin" interface, and add those classes to it's classPath. Essentially, this should allow the user of the site to upload, enable, and configure plugins.

Is this possible? And if so, how can I add these uploaded JARs to the classpath for the webapp in a way that they won't be deleted next time I deploy my WAR file (ie: I can't store the uploaded JARs in the WEB-INF folder, etc.)?

thanks,
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
That really sounds like a job for a separate class loader. That way you could avoid all sorts of ghastly duplicate class name etc problems.

Be warned, doing your own class loader is not for the faint-hearted.

Runtime scanning classes for annotations indicating a plug-in architecture is what the Jersey project does.

Letting users run arbitrary code on your server sounds like a recipe for security disaster to me - you may find yourself originating spam email or having your files deleted.

Bill

Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
Thanks William!

Letting users run arbitrary code on your server sounds like a recipe for security disaster to me - you may find yourself originating spam email or having your files deleted.


I totally agree. This is more an admin console scenario, where the user would be running arbitrary code on their own server :-)
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
OK, assuming you are going to try this, the essential thing is to define an interface for your "plug-in" which will be the only communication between the servlet side and the plugin.

Not necessarily a formal Interface, an Abstract class which the user will have to extend works also.

That will make it possible for a custom class loader to create instances and route requests to it.

Bill
Bnaresh Yad
Greenhorn

Joined: Jan 27, 2010
Posts: 4
Philippe Desrosiers wrote:Not sure this is the right forum for this question, but here goes...

I need to develop a web app (Spring MVC, if you must know, but the question is not spring-specific), that will allow a user to upload a JAR file that they have developed. The servlet needs to scan the JAR for compatible classes that implement a specific "plugin" interface, and add those classes to it's classPath. Essentially, this should allow the user of the site to upload, enable, and configure plugins.

Is this possible? And if so, how can I add these uploaded JARs to the classpath for the webapp in a way that they won't be deleted next time I deploy my WAR file (ie: I can't store the uploaded JARs in the WEB-INF folder, etc.)?

thanks,
Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
Bnaresh, my man! Thanks so much, this is pretty much exactly what I was looking for!

So that's "part one" of the problem solved. Part two is "scanning" a particular JAR file for any classes that implement a specific interface. I'm sure I've asked that question before, though, somewhere in the forums. Just have to try and find it now....


thanks again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add new jar files to class path dynamically at runtime?
 
Similar Threads
dynamic WEB-INF/lib
Why do my JARs have to be in the server classpath?
Filtering File Uploaded to server
Uploading and saving file
loading JAR